添加日期时间和时间 [英] Add datetime and time

查看:50
本文介绍了添加日期时间和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

服务器:SQL Server 2012;SP1;开发者版

Server: SQL Server 2012; SP1; Developer Edition

代码:

declare @datetime datetime = '1900-01-01 00:00:00.000'

declare @time time = '11:11:11'

select @datetime + @time

当我在 MASTER 数据库中运行上述代码时,出现错误:

When I run the above code in the MASTER database, I get the error:

Msg 402, Level 16, State 1, Line 3 数据类型日期时间和时间在 add 运算符中不兼容.

Msg 402, Level 16, State 1, Line 3 The data types datetime and time are incompatible in the add operator.

但是当它是任何其他数据库时,它就可以工作!知道为什么会发生这种情况吗?

But when it's any other database, it works! Any idea why this must be happening?

附言- 在企业版中,无论数据库上下文如何,这都会引发错误.

P.S. - In the enterprise edition, this throws an error irrespective of the database context.

推荐答案

以上代码适用于 MS SqlServer 2008,但此代码不适用于 MS SqlServer 2012 或更高版本.我遇到了同样的问题并像这样解决了它.

The above code will work with MS SqlServer 2008 but this code won't work with MS SqlServer 2012 or above. I have encountered the same issue and solve it like this.

DECLARE @today_start datetime
DECLARE @dail_time time   

SELECT @today_start = convert(datetime, @dayStr,103) + CAST(@dail_time as DATETIME)

这篇关于添加日期时间和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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