Whats the best way to get the past 24 hour results in T-SQL?

The first version is not accurate.

WHERE DateDiff(hh, CreatedDate, GETDATE()) > 24

This will return values somewhere between from 23.0001 hours ago to 24.9999 hours ago because you are counting "boundaries crossed", not an actual 24-hour period. Consider that from 1:59:59 to 2:00:00 is only one second, but DateDiff by hours will return 1 hour. Similarly, 1:00:00 to 2:59:59 is almost 2 hours, but DateDiff by hours returns the same 1 hour.

The second version is correct.

WHERE CreatedDate >= DateAdd(hh, -24, GETDATE())

Subtracting 24 hours from the current date will yield a time exactly 24.0 hours ago, to the millisecond. This will thus return 24 hours of data.

Also, the first version would be bad even if it was what you wanted because the engine would have to perform date math on every single row in the entire table, making any potential index useless and consuming a bunch of needless CPU. Instead, do the math on the opposite side of the expression from the column name. To duplicate the logic of your first expression without the performance penalty would look like this:

WHERE CreateDate >= DateAdd(hh, DateDiff(hh, 0, GETDATE()) - 24, 0)

Example:

  • GetDate() = '20100720 17:52'
  • DateDiff(hh, 0, '20100720 17:52') = 969065
  • DateAdd(hh, 969065 - 24, 0) = '20100719 17:00'

and to prove this is the same as your first expression:

  • DateDiff(hh, '20100719 17:00', '20100720 17:52') = 24

Avoid the first version. First, because it disables index utilization. The second (functional) issue with the first version is, DATEDIFF(HOUR...) returns all values less than 25 hours. Try this for clarity:

SELECT DATEDIFF(HOUR, '2010-07-19 00:00:00', '2010-07-20 00:59:59.99')