I'm trying to write a case statement in the where clause for a query I am working on. I am importing the code in Crystal reports and I am basically trying to say if the variable 'type' is set to 'create' run for this date range in the where clause else run for a different date range. It keeps giving me an error. I can't seem to identify what is wrong with my syntax here. Help anyone?
DECLARE @Date1 DATETIME
DECLARE @Date2 DATETIME
DECLARE @type VARCHAR(20)
SET @Date1 = '2010-1-1'
SET @Date2 = '2010-2-1'
SET @type = '{?DateType}'
select *
from filled
WHERE
(CASE WHEN @type = 'create' THEN
filled.CREATEDON >= @Date1
AND filled.CREATEDON < DATEADD(d, +1, @Date2)
WHEN @type <> 'create' THEN
filled.datefilled >= @Date1
AND filled.datefilled < DATEADD(d, +1, @Date2)
END)
You don't need a
case
statementThis leaves the non-sensical logic you have in the
case
statement. Both conditions have the same value for@type
. I assume that is a typo.Using a CASE expression in a where clause is possible, but generally it can be avoided, and rewritten using
AND/OR
, IN your case it would be:HOWEVER queries like this usually produce suboptimal plans. You should use
IF/ELSE
logic if possible:The reason for this is the value of @type is not known at compile time, therefore the optimiser does not know whether it will need to search on
DateFilled
orCreatedOn
, therefore cannot plan to use an index on either column (if one exists), so will do a table scan regardless of the indexes available. Whereas if you separate the logic withIF/ELSE
it does not matter what the value of @type is, a plan is created for each branch of theIF
, and in each branch the optimiser knows which column will be searched, and can plan to use the appropriate index.You can also use
UNION ALL
:Again, if indexes exist on
DateFilled
orCreatedOn
this is much more likely to produce a plan that uses them than usingOR
.