tags:

views:

40

answers:

2

Is there any way to use if-else logic in where clause in T-SQL? or do I have to implement the logic by using subquery?

+4  A: 

Use Case

(case foo when bar then baz else fizz end)
Rodrick Chapman
+4  A: 

You can use a case statement in a where clause, but it can generate performance issues, so you may want to try a different approach if you have a large dataset. A correlated subquery would not be a good alternative approach; a derived table or CTE might be.

HLGEM
+1 Actually, a CTE is a good way to go.
Rodrick Chapman