SQL,关键字'FROM'附近的CASE语句上的语法不正确

时间:2013-08-01 10:21:56

标签: sql sql-server tsql syntax case

我正在努力让下面的代码工作,单独的两段代码(在WHEN部分和ELSE部分)工作,但在此CASE语句中使用时我收到错误

  

“'CAST'附近的语法不正确,预计'AS'。”错误。

基本上,如果WHEN语句代码等于或大于24,则使用THEN语句(如果小于24)则使用ELSE语句。

在尝试了几个小时之后我似乎无法让这个工作得到任何关于我出错的地方,我将不胜感激。

SELECT CASE 
        WHEN 
            (convert(float,datediff(mi, start_work, end_work))/60) >= '24'
        THEN
            (convert(float,datediff(mi, start_work, end_work))/60)
        ELSE
            (CAST(convert(varchar(2), dateadd(minute, datediff(minute, start_time, end_time), 0), 114) 
            * 60 + RIGHT (convert(varchar(5), dateadd(minute, datediff(minute, start_time, end_time), 0), 114),
            CASE WHEN CHARINDEX(':',convert(varchar(5), dateadd(minute, datediff(minute, start_time, end_time), 0), 114)) > 0
            THEN LEN(convert(varchar(5), dateadd(minute, datediff(minute, start_time, end_time), 0), 114))-3
            ELSE LEN(convert(varchar(5), dateadd(minute, datediff(minute, start_time, end_time), 0), 114))
            END) AS decimal) / 60
FROM  NDB.dbo.statusa 
INNER JOIN NDB.dbo.details ON statusa.vkey = details.vkey
INNER JOIN NDB.dbo.chegu ON statusa.ckey = NDB.dbo.chegu.gkey
WHERE start_time!= end_time AND string1 = Visit_Id and NDB.dbo.chegu.name = 'loft'
     AS [Working]

9 个答案:

答案 0 :(得分:14)

您需要关闭case声明

 case when ... then ... else ... end

答案 1 :(得分:10)

END子句之前应该有FROM,您还应该在(之前删除CAST

答案 2 :(得分:5)

没有右括号。

SELECT CASE 
    WHEN 
        (convert(float,datediff(mi, start_work, end_work))/60) >= '24'
    THEN
        (convert(float,datediff(mi, start_work, end_work))/60)
    ELSE
        (CAST(convert(varchar(2), dateadd(minute, datediff(minute, start_time, end_time), 0), 114) 
        * 60 + RIGHT (convert(varchar(5), dateadd(minute, datediff(minute, start_time, end_time), 0), 114),
        CASE WHEN CHARINDEX(':',convert(varchar(5), dateadd(minute, datediff(minute, start_time, end_time), 0), 114)) > 0
        THEN LEN(convert(varchar(5), dateadd(minute, datediff(minute, start_time, end_time), 0), 114))-3
        ELSE LEN(convert(varchar(5), dateadd(minute, datediff(minute, start_time, end_time), 0), 114))
        END) AS decimal) / 60)

答案 3 :(得分:5)

未闭合括号和案例陈述需要END。

SELECT CASE 
            WHEN (convert(FLOAT, datediff(mi, start_work, end_work)) / 60) >= '24'
                THEN (convert(FLOAT, datediff(mi, start_work, end_work)) / 60)
            ELSE CAST(convert(VARCHAR(2), dateadd(minute, datediff(minute, start_time, end_time), 0), 114) * 60 
                 + RIGHT(
                        convert(VARCHAR(5), dateadd(minute, datediff(minute, start_time, end_time), 0), 114), 
                        CASE 
                            WHEN CHARINDEX(':', convert(VARCHAR(5), dateadd(minute, datediff(minute, start_time, end_time), 0), 114)) > 0
                                THEN LEN(convert(VARCHAR(5), dateadd(minute, datediff(minute, start_time, end_time), 0), 114)) - 3
                            ELSE LEN(convert(VARCHAR(5), dateadd(minute, datediff(minute, start_time, end_time), 0), 114))
                        END
                        ) AS DECIMAL) / 60
        END

FROM NDB.dbo.statusa
INNER JOIN NDB.dbo.details
    ON statusa.vkey = details.vkey
INNER JOIN NDB.dbo.chegu
    ON statusa.ckey = NDB.dbo.chegu.gkey
WHERE start_time != end_time
    AND string1 = Visit_Id
    AND NDB.dbo.chegu.NAME = 'loft' AS [Working]

答案 4 :(得分:5)

我意识到这个答案为时已晚,无法获得奖励。但你的脚本太长了。这将与您尝试实现的完全相同,修复代码中的错误,只是修补错误的代码:

SELECT 
  datediff(mi, 0, end_time - start_time)/60.0%24
FROM  
 ....

答案 5 :(得分:4)

CASE表达的两种基本配方 1)简单的CASE表达式 一个简单的CASE表达式针对多个值检查一个表达式。在SELECT语句中,简单的CASE表达式只允许进行相等性检查;没有进行其他比较。一个简单的CASE表达式通过将第一个表达式与每个WHEN子句中的表达式进行比较来进行操作。如果这些表达式是等价的,则返回THEN子句中的表达式。

2)搜索CASE表达式 搜索的CASE表达式允许比较运算符,并在每个布尔表达式之间使用AND和/或OR。简单的CASE表达式仅检查等效值,不能包含布尔表达式。搜索的CASE表达式的基本语法如下所示:

在此处阅读更多内容:http://blog.sqlauthority.com/2007/04/14/sql-server-case-statementexpression-examples-and-explanation/

答案 6 :(得分:4)

代码中有2个错误 首先,在CAST前面有一个额外的括号,这是不需要的(并且永远不会关闭)。变化

(CAST(convert(varchar(2), dateadd(minute, datediff(minute, start_time, end_time), 0), 114)

CAST(convert(varchar(2), dateadd(minute, datediff(minute, start_time, end_time), 0), 114) 

第二个:在FROM关键字之前的CASE语句结束时添加END(如@gefei指出的那样)。您的最终代码应如下所示:

SELECT CASE 
        WHEN 
            (convert(float,datediff(mi, start_work, end_work))/60) >= '24'
        THEN
            (convert(float,datediff(mi, start_work, end_work))/60)
        ELSE
            CAST(convert(varchar(2), dateadd(minute, datediff(minute, start_time, end_time), 0), 114) 
            * 60 + RIGHT (convert(varchar(5), dateadd(minute, datediff(minute, start_time, end_time), 0), 114),
            CASE WHEN CHARINDEX(':',convert(varchar(5), dateadd(minute, datediff(minute, start_time, end_time), 0), 114)) > 0
            THEN LEN(convert(varchar(5), dateadd(minute, datediff(minute, start_time, end_time), 0), 114))-3
            ELSE LEN(convert(varchar(5), dateadd(minute, datediff(minute, start_time, end_time), 0), 114))
            END) AS decimal) / 60
        END
FROM  NDB.dbo.statusa 
INNER JOIN NDB.dbo.details ON statusa.vkey = details.vkey
INNER JOIN NDB.dbo.chegu ON statusa.ckey = NDB.dbo.chegu.gkey
WHERE start_time!= end_time AND string1 = Visit_Id and NDB.dbo.chegu.name = 'loft'
     AS [Working] 

答案 7 :(得分:2)

RIGHT()函数没有第二个参数

RIGHT (convert(varchar(5), dateadd(minute, datediff(minute, start_time, end_time), 0), 114),

或:你错过了AS十进制后面的结束括号

END ) AS decimal ) ) / 60

答案 8 :(得分:2)

以下是您的代码的更正版本:

SELECT CASE 
        WHEN 
            (convert(float,datediff(mi, start_work, end_work))/60) >= '24'
        THEN
            (convert(float,datediff(mi, start_work, end_work))/60)
        ELSE
            (CAST(convert(varchar(2), dateadd(minute, datediff(minute, start_time, end_time), 0), 114) 
            * 60 + RIGHT (convert(varchar(5), dateadd(minute, datediff(minute, start_time, end_time), 0), 114),
            CASE WHEN CHARINDEX(':',convert(varchar(5), dateadd(minute, datediff(minute, start_time, end_time), 0), 114)) > 0
            THEN LEN(convert(varchar(5), dateadd(minute, datediff(minute, start_time, end_time), 0), 114))-3
            ELSE LEN(convert(varchar(5), dateadd(minute, datediff(minute, start_time, end_time), 0), 114))
            END) AS decimal) / 60 
        END
AS [Working]
FROM  NDB.dbo.statusa  INNER JOIN NDB.dbo.details ON statusa.vkey = details.vkey INNER JOIN NDB.dbo.chegu ON statusa.ckey = NDB.dbo.chegu.gkey WHERE start_time!= end_time AND string1 = Visit_Id and NDB.dbo.chegu.name = 'loft'

基本上,外部SELECT..CASE语句中缺少END,并且选中的列别名[Working]被错误地放在查询的末尾而不是查询的select子句中。