Omitting the Milliseconds in a Date

后端 未结 7 559
梦如初夏
梦如初夏 2020-12-09 15:16

When I select from SQL Server, I want to get a date, but omit the millisecond value, and I want it to be as a date type. So if I have a value 1/1/2009 1:23:11.923

7条回答
  •  离开以前
    2020-12-09 16:16

    Use:

    SELECT CONVERT(DATETIME, CONVERT(VARCHAR(19), GETDATE(), 120))
    

    This:

    CONVERT(VARCHAR(19), GETDATE(), 120)
    

    ...omits the milliseconds, returning a VARCHAR. So you CAST/CONVERT that into a DATETIME in order to work with the desired data type.

    See this link for a list of various date/time formats you can work with.

提交回复
热议问题