Truncate date to only hour / minute

后端 未结 3 1519
死守一世寂寞
死守一世寂寞 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:31

    I'm using something like this:

    DATEADD(hour,DATEDIFF(hour,'2000-01-01 00:00',[timestamp]),'2000-01-01 00:00')
    

    You can use this formula to truncate to other levels, ie minutes:

    DATEADD(minutes,DATEDIFF(minutes,'2000-01-01 00:00',[timestamp]),'2000-01-01 00:00')
    

    You must remember that if you are truncating to seconds level, the maximum difference between the base date and [timestamp] is 68 years.

提交回复
热议问题