11

I am trying to get the below code to work, individually the two pieces of code (in the WHEN part and the ELSE part) work but when used in this CASE statement I get an error

"Incorrect syntax near 'CAST', expected 'AS'." error.

Basically if the WHEN statements code is equals to or greater than 24 then use the THEN statement if its is under 24 then use the ELSE statement.

I cannot seem to get this to work after trying for several hours any indication as to where I am going wrong would be greatly appreciated.

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]
4

9 回答 9

14

你需要结束你的case陈述

 case when ... then ... else ... end
于 2013-08-01T10:25:51.553 回答
10

There should be an END before the FROM clause and you should also remove( before CAST.

于 2013-08-01T10:34:25.610 回答
5

未闭合的括号和 case 语句需要一个 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]
于 2015-04-12T18:16:58.370 回答
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)
于 2015-04-12T12:21:24.450 回答
5

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

SELECT 
  datediff(mi, 0, end_time - start_time)/60.0%24
FROM  
 ....
于 2015-04-17T07:55:37.097 回答
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/

于 2015-04-13T17:24:55.053 回答
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] 
于 2015-04-17T00:51:50.117 回答
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 子句中。

于 2015-04-16T08:26:38.380 回答
2

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

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

或:您错过了 AS 小数后面的结束括号

END ) AS decimal ) ) / 60
于 2015-04-10T12:30:26.100 回答