Rounding a datetime value down to the nearest half hour

前端 未结 6 611
再見小時候
再見小時候 2021-01-11 13:11

I have a requirement to round a datetime2 value down to the nearest half hour. For example \'10/17/2013 12:10:00.123\' would round down to \'10/17/2013 12:00:00.0\' And \'10

6条回答
  •  渐次进展
    2021-01-11 14:05

    How about this

    declare @d datetime = '2013-05-06 12:29.123'
    select 
    case 
        when datepart(minute, @d) < 30 then cast(dateadd(minute, -datepart(minute,@d)-datepart(second,@d), @d) as smalldatetime)
        when datepart(minute, @d) >= 30 then cast(dateadd(minute, -datepart(minute,@d)-datepart(second,@d)+30, @d) as smalldatetime)
    end
    

提交回复
热议问题