Using the CASE expression in the where clause is possible, but in general it can be avoided and rewritten with AND/OR . In your case, it will be:
WHERE( @Type = 'create' AND filled.CREATEDON >= @Date1 AND filled.CREATEDON < DATEADD(d, +1, @Date2) ) OR ( @Type != 'create' AND filled.datefilled >= @Date1 AND filled.datefilled < DATEADD(d, +1, @Date2) )
NEVER queries like this usually produce suboptimal plans. You should use IF/ELSE logic if possible:
IF @Type = 'create' BEGIN SELECT * FROM Filled WHERE Filled.CreatedOn >= @Date1 AND Filled.CreatedOn < DATEADD(DAY, 1, @Date2) END ELSE BEGIN SELECT * FROM Filled WHERE Filled.DateFilled >= @Date1 AND Filled.DateFilled < DATEADD(DAY, 1, @Date2) END
The reason for this is that the @type value is not known at compile time, therefore the optimizer does not know whether it will need to search by DateFilled or CreatedOn , therefore it cannot plan to use the index on any of (if one exists), then the table will be scanned regardless available indexes. If you separate the logic using IF/ELSE , it does not matter what the @type value is, a plan is created for each IF branch, and in each branch the optimizer knows which column will be searched, and can plan to use the corresponding index.
You can also use UNION ALL :
SELECT * FROM Filled WHERE Filled.CreatedOn >= @Date1 AND Filled.CreatedOn < DATEADD(DAY, 1, @Date2) AND @Type = 'create' UNION ALL SELECT * FROM Filled WHERE Filled.DateFilled >= @Date1 AND Filled.DateFilled < DATEADD(DAY, 1, @Date2) AND @Type <> 'create';
Again, if indexes exist on DateFilled or CreatedOn , it is much more likely to result in a plan that uses them than when using OR .