MattTheHack
MattTheHack

Reputation: 1384

Rounding up DATEDIFF, TSQL

I tried using CEILING to round up a DATEDIFF value but still got zero:

SELECT (CEILING(DATEDIFF(DAY, '2016-04-02T04:59:59', '2016-04-02T05:59:59')))

Is it possible to round up DATEDIFF?

Upvotes: 0

Views: 2222

Answers (1)

Hart CO
Hart CO

Reputation: 34774

DATEDIFF() returns 0 when using day if the two dates are the same day. You need to use a smaller time increment and divide the result of the DATEDIFF(), Hour and 24, Minute and 1440, Second and 86,400, etc:

SELECT CEILING(DATEDIFF(Second, '2016-04-02T04:59:59', '2016-04-02T05:59:59')/(24.0*60*60)) 

Note: Divide the DATEDIFF() result by a decimal value otherwise it will return an integer.

Upvotes: 2

Related Questions