Search code examples
sqldatetimegetdate

When Running a report, how do I exclude 12:00:00.000 when searching a datetime range


I am trying to run a report to return everything with a date of "tomorrow" but I want to include from 12:01:00.000 to 11:59:00.000 in that date. i can only return from current datetime and forward 24 hours.

AND e.event_date >= getdate()+2
AND e.event_date < getdate()+3

How do I write this?


Solution

  • Assuming you are creating your reports via T-SQL, this will do the trick:

    AND e.event_date >= DATEADD(DAY, 1, CONVERT(DATE, GETDATE()))
    AND e.event_date < DATEADD(DAY, 2, CONVERT(DATE, GETDATE()))
    

    This will filter all entries with event_date of tomorrow.

    You cannot get the current date directly in SQL, but you can convert the current DateTime to a Date object which will strip off the information about time.

    If you want to exclude 12:00:00.000 (Why would you?), just modify your comparer:

    AND e.event_date > DATEADD(DAY, 1, CONVERT(DATE, GETDATE()))