TSQL where case
10,021
Solution 1
select *
from [table]
where case when var_c = 'something' then var_a else var_b end = '10'
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
Related videos on Youtube
Author by
user1723982
Updated on June 17, 2022Comments
-
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'
usevar_A='10'
elsevar_B='10'