TSQL, where register

I need the correct tsql syntax for this problem:

Select * from table where var_A='10'
select * from table where var_B='10'

      

When to use var_A or var_B depends on the value of var_C

So when var_c='something'

use var_A='10'

elsevar_B='10'

+3


source to share


3 answers


select *
from [table]
where case when var_c = 'something' then var_a else var_b end = '10'

      



SQL Fiddle with demo .

+4


source


Ian probably has a better answer, but I thought I would give another answer for a different (albeit less flexible) method in case it helps:



IF var_c = 'something' BEGIN
    SELECT * FROM [table] WHERE var_a = '10'
END
ELSE BEGIN
    SELECT * FROM [table] WHERE var_b = '10'
END

      

+2


source


This is how you use a case statement inside a where clause.

select * from table
where ( case when var_c = 'something' 
        then var_a
        else 
            var_b
        end
      )

      

0


source