loli
loli

Reputation: 31

use if-else logic in where clause in T-SQL

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?

Upvotes: 3

Views: 727

Answers (2)

HLGEM
HLGEM

Reputation: 96572

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.

Upvotes: 4

Rodrick Chapman
Rodrick Chapman

Reputation: 5543

Use Case

(case foo when bar then baz else fizz end)

Upvotes: 4

Related Questions