Rounding a datetime value down to the nearest half hour

前端 未结 6 608
再見小時候
再見小時候 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:07

    select cast(floor(cast(
        cast('10/17/2013 12:34:00' as datetime) 
    as float(53)) * 48) / 48 as datetime)
    

    EDIT

    Works better if you use smalldatetime to avoid the extra precision

    select cast(floor(cast(
        cast('2012-01-02 11:33:14.097' as smalldatetime) 
    as float(53)) * 48) / 48 as smalldatetime)
    

提交回复
热议问题