Additional criteria are just needed for processing when one or the other is NULL :
AND ( (DateCreated >= @DateFrom and DateCreated < DATEADD(day,1,@DateTo)) OR (@DateFrom IS NULL AND @DateTo IS NULL) OR (@DateFrom IS NULL AND DateCreated < DATEADD(day,1,@DateTo)) OR (@DateTo IS NULL AND DateCreated >= @DateFrom) )
Edit: Giorgi's approach was simpler, here it is adapted for use with DATETIME :
AND ( (DateCreated >= @DateFrom OR @DateFrom IS NULL) AND (DateCreated < DATEADD(day,1,@DateTo) OR @DateTo IS NULL) )
The problem with BETWEEN or <= when using the DATE variable in the DATETIME field is that any time after midnight on the last day will be excluded.
'2015-02-11 13:07:56.017' more '2015-02-11' Instead of distinguishing your field as DATE for comparison, it is better for performance to add a day to your variable and change from <= to < .
source share