为什么可以从 DATETIME 中减去整数而不是 DATE 类型 [英] Why can integer be subtracted from DATETIME but not DATE type

查看:19
本文介绍了为什么可以从 DATETIME 中减去整数而不是 DATE 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是DATETIME & 的关系吗?整数和日期 &整数一致?

Is the relationship between DATETIME & INTEGER and DATE & INTEGER consistent?

这执行得很好:

DECLARE @Yesterday DATETIME = GETDATE();
SELECT @Yesterday-1;

就像这样:

DECLARE @Yesterday DATE = GETDATE();
SELECT @Yesterday;

这个错误:

DECLARE @Yesterday DATE = GETDATE();
SELECT @Yesterday-1;

我可以安全地从 datetime 中减去 integer 类型,但不能从 date 中减去.
这种行为的原因是什么?

I can safely subtract an integer type from a datetime but not from a date.
What is the reason for this behaviour?

推荐答案

其实还是很一致的.DATETIME 是从以前版本的 sql server 继承的类型.从 2008 版开始,引入了 DATETIME2,并删除了加减整数的可能性.您仍然可以在 DATETIME 上将其作为遗留.

Actually, it is very consistent. DATETIME is a type inherited from previous editions of sql server. Since 2008 edition, DATETIME2 has been introduced, and possibility of adding/subtracting integers removed. You can still do it on DATETIME as a legacy.

DATEDATETIME2 一样,也是从 2008 年开始出现的,而且这种类型的加减数字也是被禁止的.

DATE, like DATETIME2 had been around since 2008, too, and for this type adding/subtracting numbers is also prohibited.

这会给你一个错误:

DECLARE @Yesterday DATETIME2 = GETDATE();
SELECT @Yesterday-1;

所以一切都很好:)

这篇关于为什么可以从 DATETIME 中减去整数而不是 DATE 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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