简体   繁体   English

在T-SQL中减去两个日期

[英]Subtraction of two dates in t-sql

I was going through a script written by a predecessor. 我正在看一个前辈写的剧本。

Can someone explain to me why would this statement 有人可以向我解释为什么这句话

--- CreatedDateTime is a datetime column in SALES_ORDER table.
SELECT * FROM SALES_ORDER
WHERE GETDATE() - CreatedDateTime < 1

returns the same result as 返回与以下结果相同的结果

SELECT * FROM SALES_ORDER 
WHERE DateDiff(hh,CreatedDateTime, GetDate()) < 24

Subtraction of a number from a DATETIME is documented here : "Can also subtract a number, in days, from a date." 从DATETIME减去数字的记录在此处 :“也可以从日期中减去以天为单位的数字。”

declare @Now as DateTime = GetDate();
declare @OneWeekAgo as SQL_Variant = @Now - 7;

select @Now as [Now], @OneWeekAgo as [Delta], SQL_Variant_Property( @OneWeekAgo, 'BaseType' ) as [Data Type];

Under Using Operators with Date and Time Data Types : "To add and subtract for all date and time data types, use DATEADD and DATEDIFF." 对日期和时间数据类型使用运算符下:“要对所有日期和时间数据类型进行加减,请使用DATEADD和DATEDIFF。

In a possible violation of the Principle of Least Astonishment we see the following curious result: 在违反最小惊讶原则的情况下,我们看到以下奇怪的结果:

declare @Now as DateTime = GetDate();
declare @Then as DateTime = '17760704';
declare @Delta as SQL_Variant = @Now - @Then;

select @Now as [Now], @Then as [Then], @Delta as [Delta],
  SQL_Variant_Property( @Delta, 'BaseType' ) as [Data Type],
  Cast( @Delta as Int ) as [Days];

Aaron Bertrand Clause : The information provided is unapproved by Aaron Bertrand. Aaron Bertrand条款 :提供的信息未经Aaron Bertrand的认可。 Additionally, the author has failed to indicate all possible ways in which it may be inapplicable or less-than-optimal in any given environment, no matter how obscure or contrived. 此外,作者未能指出在任何给定环境中它可能不适用或不达最佳效果的所有可能方式,无论其晦涩或人为。 The author has also made the cardinal and/or ordinal sin of failing to explicitly reference a minimum of three (3) of Aaron Bertrand's blog posts and canonical answers. 作者还犯了主要和/或序贯的罪过,即没有明确引用至少三(3)个亚伦·伯特兰(Aaron Bertrand)的博客文章规范的答案。 Thus it offers no benefit to the community at large and the author should be immediately and permanently banished from all StackExchange sites and any content provided by the author should be removed therefrom. 因此,它对整个社区没有任何好处,因此应立即从所有StackExchange网站上永久逐出作者,并应从其中删除作者提供的任何内容。 It matters not a whit the extent to which Microsoft's splendid documentation may have contributed to any (mis)understanding. 微软出色的文档在多大程度上有助于任何(误)理解,都无关紧要。

Both boolean expressions evaluate to the same thing. 这两个布尔表达式的值都相同。 Either it's 24 hours old or 1 day old. 是24小时还是1天。 Incidentally both of them exclude the usage of an index on the CreatedDateTime column. 两者都顺便排除了CreatedDateTime列上索引的使用。 If you have an index on this column and would like to increase the likelihood of it being used then you would write it more like this: 如果您在此列上有一个索引,并且想增加使用它的可能性,那么您可以这样写:

    SELECT * FROM SALES_ORDER
    WHERE CreatedDateTime > GETDATE() - 1

or this (less than one day old): 或以下内容(不到一天):

    SELECT * FROM SALES_ORDER
    WHERE CreatedDateTime > DateAdd(dd,-1,GetDate())

or this (less than 86400000 milliseconds old old): 或以下(旧的小于86400000毫秒):

    SELECT * FROM SALES_ORDER
    WHERE CreatedDateTime > DateAdd(ms,-86400000,GetDate())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM