Search code examples
sql

Return entries within last 24 hours of a specific date time in SQL


I have a dataset which contains two different datetimes - datetime1 and datetime2.

I would like to return all the entries where the datetime2 has occurred in the last 24 hours of datetime1.

I tried this query below but gives me all the entries.

WHERE datetime2 <= DATEADD(HOUR, 24, datetime1)

Is there a function that will return the entries with datetime2 that occurred in the past 24 hours of datetime1?


Solution

  • Try this:

    WHERE datetime2 BETWEEN DATEADD(HOUR, -24, datetime1) AND datetime1