I am using SQLServer 2008 and came across this problem. I have a function where a user can select multiple dates from a menu. They also have the option to tick a checkbox ‘select all dates’.
If they select the checkbox my query should only return results in the given date. Thus execution a WHERE-clause.
If they did not select the checkbox but selected one or more dates; the query should be executed with a where clause.
I have 2 parameters for this.
-SelectedDates(eg. ‘2012-11-15′,’2012-11-15’)
-AllDates (‘X’ or ”)
Normally the query whould look like this:
SELECT * FROM x
WHERE x.date in ([Param.1])
So my question: how do I adapt the query in case the WHERE-clause is not needed.
FYI: the [Param.1] notation is used in the environment I am working in.
simple, use a OR clause, if alldates is checked it will return data from all dates like this:
if Param.2 is checked (‘X’) it will ignore the result of the first condition cause you are using the OR clause.