question

wolfeste avatar image
wolfeste asked

Case Statement in Where clause

Is there a way to write a case statement so that my parameter will choose the correct "clause" to input? For example below, this is the best way to describe. Select Name From Customers Where Hide = '0' and Case When @Var1 = 1 Then AND FirstName IS NULL When @Var1 = 2 Then AND FirstName IS NOT NULL Basically want to have a parameter to choose which statement to use.
sql-server-2012
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Tom Staab avatar image
Tom Staab answered
I don't think CASE is the best approach here. I realize your real situation might be more complex than your example, but I'll just respond to your example right now. Please respond with comments or update your original question if you have any follow-up questions. SELECT c.Name FROM Customers c WHERE c.Hide = '0' AND ( (@Var1 = 1 AND c.FirstName is null) OR (@Var1 = 2 AND c.FirstName is not null) ) ;
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

wolfeste avatar image
wolfeste answered
Exactly what i was looking for. Thank you!
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.