-1

我很尴尬地说这是我在 SQL Server 中的弱点,我不知道如何让这段代码正常运行。它是几个不同的代码之一:

SUM(CASE WHEN RRDD Not Like '12%' AND RRDD Not Like '13%' AND RRDD Not Like '16%' AND RRDD
Not Like '17%' AND RRDD Not Like '2706%' AND RRDD Not Like '2707%' AND RRDD Not Like '2331%'
THEN CAST([2011 Total] AS FLOAT)*CASE WHEN IsNumeric [DUR_In_Hours] = 1 THEN
CAST([DUR_IN_Hours] AS FLOAT) ELSE 0 END) AS SP_DOM_COST

我得到的错误如下:

IsNumeric is not recognized and incorrect syntax near DUR_In_Hours

4

1 回答 1

2

您缺少括号IsNumeric()

 WHEN IsNumeric([DUR_In_Hours]) = 1 

你也错过了END你的第一个CASE

SUM(CASE 
        WHEN RRDD Not Like '12%' 
            AND RRDD Not Like '13%' 
            AND  RRDD Not Like '16%' 
            AND RRDD Not Like '17%' 
            AND RRDD Not Like '2706%' 
            AND RRDD Not Like '2707%' 
            AND RRDD Not Like '2331%' 
        THEN CAST([2011 Total] AS FLOAT)
        ELSE 0
        END
    *
    CASE 
        WHEN IsNumeric([DUR_In_Hours]) = 1 
        THEN CAST([DUR_IN_Hours] AS FLOAT) 
        ELSE 0 
        END) AS SP_DOM_COST

最后一个建议,根据RRDD字段的数据类型,您可能不需要使用LIKE语法。您可能可以将其替换为NOT IN

SUM(CASE 
        WHEN RRDD NOT IN ('12', '13', '16', '17', '2706', '2707', '2331')
        THEN CAST([2011 Total] AS FLOAT)
        ELSE 0
        END
    * 
     CASE 
        WHEN IsNumeric([DUR_In_Hours]) = 1 
        THEN CAST([DUR_IN_Hours] AS FLOAT) 
        ELSE 0 
        END) AS SP_DOM_COST

请参阅带有演示的 SQL Fiddle

于 2013-01-22T18:25:49.720 回答