How to fix this in case expression?

Viewed 49
Select 
    ID, date_d,
    Case ID 
       when ID >= 0 then "greater" 
       else "Smaller"
    End as Grtr 
from 
    emp_date

Error:

Msg 102, Level 15, State 1, Line 24
Incorrect syntax near '>'

Input

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

You need to use searched case expression(original version is mixing both simple/searched syntax):

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  

The shorthand syntax (case expression when value ...) is basically syntactic sugar over a list of equal conditions. If you want to use other operators, you need to use the full syntax (case when condition then...). E.g.:

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