Datetime conversion error for 1 sql server, but not another

前端 未结 2 736
梦毁少年i
梦毁少年i 2020-12-21 07:59

I\'ve got 2 servers running SQL Server 2008, and I have the following query:

SELECT cast(\'13/1/2011\' as datetime)

If I execute this query

2条回答
  •  旧时难觅i
    2020-12-21 08:09

    Even better, don't rely on the SET DATEFORMAT or DEFAULT_LANGUAGE settings to ensure your date conversions work as expected. In SQL Server one should always—as a matter of best practice—either use a neutral format, or use an explicit format specifier, no matter what language settings are in use or what your dates look like. Try the following on both servers and you will have no problem.

    • Date neutral format: Convert(datetime, '20110113')
    • Date neutral format: Convert(datetime, '2011-01-13T00:00:00')
    • Explicit format specifier: Convert(datetime, '13/1/2011', 103) - see SQL Server Books Online: Cast and Convert for details.

提交回复
热议问题