Imagine you want to give a user the possiblity to filter for information about which other persons in your company lives in the same city. However, instead of a explicit city name filter, you just provide him a checkbox aka Filter for Hometown. When you are now running an SQL query, you will think about adding a where clause, which depends whether or not the checkbox is selected. Now this can be quite tricky, when you do not know the syntax.
Below is an example for the AdventureWorks database.
1 2 3 4 5 6 7 |
USE AdventureWorks2008R2 DECLARE @WithCityMilwaukieFilter INTEGER = 1 SELECT * FROM Person.Address a WHERE (CASE WHEN @WithCityMilwaukieFilter = 1 THEN a.City ELSE 'Milwaukie' END = 'Milwaukie') |
Be First to Comment