TSQL where case

10,021

Solution 1

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

SQL Fiddle with demo.

Solution 2

Ian probably has the best answer available but I thought I would supply another answer for a different (although 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
Share:
10,021

Related videos on Youtube

user1723982
Author by

user1723982

Updated on June 17, 2022

Comments

  • user1723982
    user1723982 about 2 years

    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' else var_B='10'