如何在SQL Server 2008中设置DateTime变量?

问题描述:

SQL Server 2008在使用 DateTime 时没有达到我的预期。无论我使用哪种日期格式,它都不允许我设置 DateTime 变量。

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.

执行时:

DECLARE @Test AS DATETIME
SET @Test = 2011-02-15
PRINT @Test

我得到以下输出:

Jun 18 1905 12:00AM

我已经检查了所有我可以找到的区域设置一切似乎还好。我还尝试将 DateTime 设置为各种文字替代形式,例如 15/02/2011, 2011-02-15 00:00:00等。

I've checked all of the regional settings that I can find & it all appears okay. I've also tried setting the DateTime to various literal alternatives, such as '15/02/2011', '2011-02-15 00:00:00', etc.

您需要将日期时间值括在引号中:

You need to enclose the date time value in quotes:

DECLARE @Test AS DATETIME 

SET @Test = '2011-02-15'

PRINT @Test