I have a function to round a datetime to the nearest quarter hour.
But is there a method to round down to the nearest quarter instead?
Example.
08:14:00 becomes 08:00:00
08:03:00 becomes 08:00:00
08:29:00 becomes 08:15:00
08:55:00 becomes 08:45:00
This is what I have now to round to the nearest quarter.
(
@dt datetime
)
returns datetime
as
begin
declare @result datetime
declare @mm int
set @mm=datepart(minute,@dt)
set @result = dateadd(minute,-@mm + (round(@mm/cast(15 as float),0)*15) , @dt )
return @result
Using SQL Server:
The strategy is:
*24to get number of hours,*4to get number of quartersFLOOR/(24*4)smalldatetimeis used to avoid float rounding issues.This can easily be adjusted to use
ROUNDorCEILINGinstead; or to use other hour multiples instead of4(quarters).