Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TSQL case statement with multiple when values? [closed]

Is there a way to do something like this? (This is pseudo code)

      CASE(@P1) 
        WHEN 'a' or 'd' or 'z' THEN 1
        WHEN 'b' or 't' THEN 2
        ELSE 0 

The idea being that I can check multiple values that should return the same value. i.e. 'a' returns 1 and 't' returns 2

like image 880
John S Avatar asked May 14 '14 15:05

John S


1 Answers

select CASE WHEN @P1 in ('a', 'd', 'z') THEN 1
            WHEN @P1 in ('b', 't') THEN 2
            ELSE 0 
       END
from your_table

or

select CASE WHEN @P1 = 'a' or @P1 = 'd' or @P1 = 'z' THEN 1
            WHEN @P1 = 'b' or @P1 = 't' THEN 2
            ELSE 0 
       END
from your_table
like image 94
juergen d Avatar answered Nov 03 '22 00:11

juergen d