I store events in SQLServer 2005 where the time the event occured is important and must be stored in the datebase. What is the fastest way to write the date range check in the where clause to ensure everything on that day is selected?
Currently when @DateStart and @DateEnd are passed in I set @DateStart to midnight and set @DateEnd to the last instant before midnight as the very first thing to catch every possible event on the day.
IF (@DateStart IS NOT NULL)
BEGIN
SET @DateStart = CAST (
( CAST (DATEPART (yyyy,@DateStart) AS NVARCHAR(4)) +'/'+
CAST (DATEPART (mm,@DateStart) AS NVARCHAR(2)) +'/'+
CAST (DATEPART (dd,@DateStart) AS NVARCHAR(2)) +' '+
'00:00:00.000'
)
AS DATETIME)
END
IF (@DateEnd IS NOT NULL)
BEGIN
SET @DateEnd = CAST (
( CAST (DATEPART (yyyy,@DateEnd) AS NVARCHAR(4)) +'/'+
CAST (DATEPART (mm,@DateEnd) AS NVARCHAR(2)) +'/'+
CAST (DATEPART (dd,@DateEnd) AS NVARCHAR(2)) +' '+
'23:59:59.997'
)
AS DATETIME
)
END
So the where clause is very easy to read:
WHERE ( EventDate >= @DateStart AND EventDate <= @DateEnd )
Thanks,