user219628
user219628

Reputation: 3905

T-SQL datetime rounded to nearest minute and nearest hours with using functions

I would like to get datetime column rounded to nearest hour and nearest minute preferably with existing functions.

For this column value 2007-09-22 15:07:38.850, the output will look like:

2007-09-22 15:08 -- nearest minute
2007-09-22 15    -- nearest hour

Upvotes: 149

Views: 205735

Answers (6)

Charlieface
Charlieface

Reputation: 71419

In the new SQL Server 2022, and in Azure SQL, you can use the DATETRUNC function:

SELECT DATETRUNC(minute, @yourDate)
SELECT DATETRUNC(hour, @yourDate)

You can also use the DATE_BUCKET function if you want to round to an arbitrary number of hours or minutes

SELECT DATE_BUCKET(minute, 3, @yourDate)
SELECT DATE_BUCKET(hour, 3, @yourDate)

You can even give it a fourth parameter with a starting point, see the documentation.

Upvotes: 9

Though there is an accepted answer for so long here I would like to point out that OP's desired output was

2007-09-22 15:08 -- nearest minute
2007-09-22 15    -- nearest hour

So, for nearest minute I have just added 30 seconds and collected the value up to minute by converting it into varchar. And for nearest minute I have added 30 minutes and collected the value up to hour part. Voila!!!

Query:

declare @dt datetime

set @dt = '09-22-2007 15:07:38.850'
select convert(varchar(16),dateadd(SECOND,30,@dt),120) nearest_minute

Output:

nearest_minute
2007-09-22 15:08

Query:

declare @dt datetime
set @dt = '09-22-2007 15:07:28.850'
select convert(varchar(16),dateadd(SECOND,30,@dt),120) nearest_minute

Output:

nearest_minute
2007-09-22 15:07

Query:

declare @dt datetime
set @dt = '09-22-2007 15:07:38.850'
select convert(varchar(13),dateadd(MINUTE,30,@dt),120) nearest_hour

Output:

nearest_hour
2007-09-22 15

Query:

declare @dt datetime
set @dt = '09-22-2007 15:30:28.850'
select convert(varchar(13),dateadd(MINUTE,30,@dt),120) nearest_hour

Output:

nearest_hour
2007-09-22 16

fiddle

Upvotes: -1

Jam_Jam
Jam_Jam

Reputation: 43

Select convert(char(8), DATEADD(MINUTE, DATEDIFF(MINUTE, 0, getdate), 0), 108) as Time

will round down seconds to 00

Upvotes: -1

Andrew Steitz
Andrew Steitz

Reputation: 2007

I realize this question is ancient and there is an accepted and an alternate answer. I also realize that my answer will only answer half of the question, but for anyone wanting to round to the nearest minute and still have a datetime compatible value using only a single function:

CAST(YourValueHere as smalldatetime);

For hours or seconds, use Jeff Ogata's answer (the accepted answer) above.

Upvotes: 37

Jeff Ogata
Jeff Ogata

Reputation: 57783

declare @dt datetime

set @dt = '09-22-2007 15:07:38.850'

select dateadd(mi, datediff(mi, 0, @dt), 0)
select dateadd(hour, datediff(hour, 0, @dt), 0)

will return

2007-09-22 15:07:00.000
2007-09-22 15:00:00.000

The above just truncates the seconds and minutes, producing the results asked for in the question. As @OMG Ponies pointed out, if you want to round up/down, then you can add half a minute or half an hour respectively, then truncate:

select dateadd(mi, datediff(mi, 0, dateadd(s, 30, @dt)), 0)
select dateadd(hour, datediff(hour, 0, dateadd(mi, 30, @dt)), 0)

and you'll get:

2007-09-22 15:08:00.000
2007-09-22 15:00:00.000

Before the date data type was added in SQL Server 2008, I would use the above method to truncate the time portion from a datetime to get only the date. The idea is to determine the number of days between the datetime in question and a fixed point in time (0, which implicitly casts to 1900-01-01 00:00:00.000):

declare @days int
set @days = datediff(day, 0, @dt)

and then add that number of days to the fixed point in time, which gives you the original date with the time set to 00:00:00.000:

select dateadd(day, @days, 0)

or more succinctly:

select dateadd(day, datediff(day, 0, @dt), 0)

Using a different datepart (e.g. hour, mi) will work accordingly.

Upvotes: 263

Magnus
Magnus

Reputation: 46919

"Rounded" down as in your example. This will return a varchar value of the date.

DECLARE @date As DateTime2
SET @date = '2007-09-22 15:07:38.850'

SELECT CONVERT(VARCHAR(16), @date, 120) --2007-09-22 15:07
SELECT CONVERT(VARCHAR(13), @date, 120) --2007-09-22 15

Upvotes: 28

Related Questions