How to set a DateTime variable in SQL Server 2008?

前端 未结 10 1915
暗喜
暗喜 2020-12-30 20:15

SQL Server 2008 is not doing what I expected with DateTime. It doesn\'t let me set DateTime variables, no matter what date format I use.

Wh

10条回答
  •  感动是毒
    2020-12-30 20:52

    You want to make the format/style explicit and don't rely on interpretation based on local settings (which may vary among your clients infrastructure).

    DECLARE @Test AS DATETIME
    SET @Test = CONVERT(DATETIME, '2011-02-15 00:00:00', 120) -- yyyy-MM-dd hh:mm:ss
    SELECT @Test
    

    While there is a plethora of styles, you may want to remember few

    • 126 (ISO 8601): yyyy-MM-ddThh:mm:ss(.mmm)
    • 120: yyyy-MM-dd hh:mm:ss
    • 112: yyyyMMdd

    Note that the T in the ISO 8601 is actually the letter T and not a variable.

提交回复
热议问题