TSQL - Case - Values in? TSQL - Case - Values in? sql sql

TSQL - Case - Values in?


Don't put brackets around the numbers.


Use:

SELECT x = CASE              WHEN t.xvalue IN (52, 57, 82, 83) THEN 'xvalue'             WHEN t.yvalue IN (01, 02, 11) THEN 'yvalue'             ELSE NULL           END  FROM TABLE t

Assuming you want the value from the column, use:

SELECT x = CASE              WHEN t.xvalue IN (52, 57, 82, 83) THEN t.xvalue             WHEN t.yvalue IN (01, 02, 11) THEN t.yvalue             ELSE NULL           END  FROM TABLE t

You realize that if both xvalue and yvalue are in the groups, only the xvalue will be displayed?


Try this:

select case     when xvalue in (52,57,82,83)         then "xvalue"    when yvalue in (01,02,11)         then "yvalue"    else        'NULL'    end as 'x'

If you want to use the x, which I am assuming is going to be a variable you will want to define it like this:

DECLARE @x int

select @x = case     when xvalue in (52,57,82,83)         then "xvalue"    when yvalue in (01,02,11)         then "yvalue"    else        'NULL'    end