当>时出现CASE语法错误在WHEN子句中使用

时间:2017-06-08 17:39:53

标签: sql

当我没有“>”时,此查询运行正常WHEN子句中的符号但是当ProposalDate晚于'2017-04-10 00:00:00.000'时,我需要这个。我不知道为什么我在添加>时收到语法错误进入WHEN条款。

SELECT *,
    CASE ProposalDate WHEN > '2017-04-10 00:00:00.000'
        THEN (OrderTotal - ProposalWholesalePrice) +
             (ProposalWholesalePrice * 1.02)
        ELSE OrderTotal    
    END "New_OrderTotal"
FROM webservices.dbo.DT_Open_Orders_Baseprice
WHERE ProposalSerialNumber = '639036'

3 个答案:

答案 0 :(得分:2)

SQL CASE表达式has two forms

  • 简单的CASE表达式和
  • 搜索到的CASE表达式。

简单CASE,即CASE ProposalDate WHEN ...不能与比较运算符一起使用;您需要搜索CASE而不是:

SELECT
    *
,   CASE
        WHEN ProposalDate > '2017-04-10 00:00:00.000'
        THEN (OrderTotal - ProposalWholesalePrice) + (ProposalWholesalePrice * 1.02)
        ELSE OrderTotal
    END "New_OrderTotal"
FROM webservices.dbo.DT_Open_Orders_Baseprice
WHERE ProposalSerialNumber = '639036'

答案 1 :(得分:0)

你有没有试过把" WHEN"在列名之前? 例如:

SELECT *, CASE WHEN ProposalDate > '2017-04-10 00:00:00.000'

THEN (OrderTotal - ProposalWholesalePrice) + (ProposalWholesalePrice * 1.02) ELSE OrderTotal

END "New_OrderTotal" FROM webservices.dbo.DT_Open_Orders_Baseprice WHERE ProposalSerialNumber = '639036'

答案 2 :(得分:0)

简写case语法不使用任何运算符,只是使用=条件计算的一系列值。如果要使用其他运算符,则必须使用完整的case语法:

SELECT *, 
       CASE WHEN ProposalDate > '2017-04-10 00:00:00.000' 
            THEN (OrderTotal - ProposalWholesalePrice) + 
                 (ProposalWholesalePrice * 1.02) 
           ELSE OrderTotal 
       END "New_OrderTotal"
FROM   webservices.dbo.DT_Open_Orders_Baseprice 
WHERE  ProposalSerialNumber = '639036'