How to set a DateTime variable in SQL Server 2008?

前端 未结 10 1882
暗喜
暗喜 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 21:04

    You Should Try This Way :

      DECLARE @TEST DATE
      SET @TEST =  '05/09/2013'
      PRINT @TEST
    
    0 讨论(0)
  • 2020-12-30 21:09

    First of all - use single quotes around your date literals!

    Second of all, I would strongly recommend always using the ISO-8601 date format - this works regardless of what your locale, regional or language settings are on your SQL Server.

    The ISO-8601 format is either:

    • YYYYMMDD for dates only (e.g. 20110825 for the 25th of August, 2011)
    • YYYY-MM-DDTHH:MM:SS for dates and time (e.g. 2011-08-25T14:15:00 for 25th of AUgust, 14:15/2:15pm in the afternoon)
    0 讨论(0)
  • 2020-12-30 21:12

    You need to enclose the date time value in quotes:

    DECLARE @Test AS DATETIME 
    
    SET @Test = '2011-02-15'
    
    PRINT @Test
    
    0 讨论(0)
  • 2020-12-30 21:14

    Try using Select instead of Print

    DECLARE @Test AS DATETIME 
    
    SET @Test = '2011-02-15'
    
    Select @Test
    
    0 讨论(0)
提交回复
热议问题