Quantcast
Channel: SQL Server Blog
Viewing all articles
Browse latest Browse all 1849
↧

Round a datetime to the nearest minute (or down to the minute)

$
0
0
Last week I was doing a comparison of 2 datetime fields, but I only wanted to do the comparison on the minute level. So I needed something to round the datetime to the nearest minute or down to the minute.

I found this great answer on SO:

DECLARE @dt DATETIME
SET @dt = '09-22-2007 15:07:38.850'
SELECT DATEADD(mi, DATEDIFF(mi, 0, @dt), 0)

And if you want to round up or down, you can add 30 seconds to the parameter like this:
SELECT DATEADD(mi, DATEDIFF(mi, 0,Β DATEADD(s, 30, @dt)), 0)


↧

Viewing all articles
Browse latest Browse all 1849


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>