【问题标题】:How to fix this in case expression?在 case 表达式中如何解决这个问题?
【发布时间】:2019-07-01 19:08:32
【问题描述】:
Select 
    ID, date_d,
    Case ID 
       when ID >= 0 then "greater" 
       else "Smaller"
    End as Grtr 
from 
    emp_date

错误:

消息 102,第 15 级,状态 1,第 24 行
'>'

附近的语法不正确

输入

ID      date_d
----------------
1   2018-02-02
2   2018-03-31
3   2019-03-31

【问题讨论】:

  • Select ID,date_d, Case when ID > 0 then 'greater' When ID =2 then 'between' Else 'Smaller' End as Grtr from emp_date ----------- -------------------------------------------------- --------------输出 1 2018-02-02 更大 2 2018-03-31 更大 3 2019-03-31 更大

标签: sql sql-server syntax syntax-error case


【解决方案1】:

您需要使用搜索案例表达式(原始版本混合了简单/搜索语法):

Select ID,date_d, Case  when ID >= 0 then 'greater' Else 'Smaller' End as Grtr 
from emp_date

CASE:

Searched CASE expression:  
CASE  
     WHEN Boolean_expression THEN result_expression [ ...n ]   
     [ ELSE else_result_expression ]   
END  

【讨论】:

【解决方案2】:

简写语法 (case expression when value ...) 基本上是一系列相等条件的语法糖。如果要使用其他运算符,则需要使用完整语法 (case when condition then...)。例如:

Select ID,date_d,
Case 
   when ID >= 0 then 'Greater'
    Else 'Smaller'
    End as Grtr 
from emp_date

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    相关资源
    最近更新 更多