TSQL case statement with multiple when values?

16,963
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
Share:
16,963
John S
Author by

John S

Been around since the days of Business Basic and the 6502. Currently working mostly in the Microsoft stack, C#, SQL, etc.. Also work with SCSS, CSS, JQuery, Javascript. Currently a Software Engineer that likes to keep up on some aspects of the IT side of things so that I can do a better job writing my software. Always striving to understand the whole problem domain not just the algorithm.

Updated on June 08, 2022

Comments

  • John S
    John S about 2 years

    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