Truncate Datetime to Second (Remove Milliseconds) in T-SQL

后端 未结 8 1493
庸人自扰
庸人自扰 2020-12-01 08:59

What is the best way to shorten a datetime that includes milliseconds to only have the second?

For example 2012-01-25 17:24:05.784 to 2012-01-25

8条回答
  •  爱一瞬间的悲伤
    2020-12-01 09:36

    This will truncate the milliseconds.

    declare @X datetime
    set @X = '2012-01-25 17:24:05.784'
    select convert(datetime, convert(char(19), @X, 126))
    

    or

    select dateadd(millisecond, -datepart(millisecond, @X), @X)
    

    CAST and CONVERT
    DATEADD
    DATEPART

提交回复
热议问题