如何在SQL Server 2008中设置DateTime变量? [英] How to set a DateTime variable in SQL Server 2008?

查看:120
本文介绍了如何在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

这篇关于如何在SQL Server 2008中设置DateTime变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆