Home Using SQL Server CASE statement in WHERE
Reply: 0

Using SQL Server CASE statement in WHERE

user703 Published in August 16, 2018, 6:17 am

I want to select records from a table in a stored procedure. Given parameters can be empty or a string including some keys separated by comma (1, 2, etc) I want to manage that when a parameter is an empty string, "WHERE" ignore searching. I'm using this code:

where (CASE when @PatientID <> 0 then ( dental.ID_Sick in (1,2)) else (1=1) end)

Something like that is working in W3School. I mean:

SELECT * FROM Customers
WHERE (case when 1=1 then (Country IN ('Germany', 'France', 'UK')) else 1=1 end);

What is the problem in my query that does not work? SQLServerManagementStudio is giving error on "IN" statement.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.361966 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO