Truncate date to only hour / minute

后端 未结 3 1592
死守一世寂寞
死守一世寂寞 2021-02-02 09:39

How do i trunc the date in sql server 2008 like this :

I have 2012-01-02 12:04:11.443 and I want only to select 2012-01-02 12:00:00.000 and

3条回答
  •  忘掉有多难
    2021-02-02 10:10

    This seems to work:

    select [Rounded Time] =
        dateadd(mi,datediff(mi,0,dateadd(ss,30,a.DT)),0)
    
    from
        (
        select dt = convert(datetime,'8:24:29.997')
        union all
        select dt = convert(datetime,'8:24:30.000')
        ) a
    

    Results:

    Rounded Time                                           
    ------------------------------------------------------ 
    1900-01-01 08:24:00.000
    1900-01-01 08:25:00.000
    
    (2 row(s) affected)
    

提交回复
热议问题