Case TSQL statement with multiple values?

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 is that I can check for multiple values ​​that should return the same value. that is, 'a' returns 1 and 't' returns 2

+4
source share
1 answer
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
+14
source

Source: https://habr.com/ru/post/1540543/


All Articles