在 SQL Server 中删除日期时间的时间部分的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1177449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 02:57:13  来源:igfitidea点击:

Best approach to remove time part of datetime in SQL Server

sqlsql-servertsqldatetimedate

提问by Stephen Perelson

Which method provides the best performance when removing the time portion from a datetime field in SQL Server?

从 SQL Server 中的日期时间字段中删除时间部分时,哪种方法可提供最佳性能?

a) select DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)

or

或者

b) select cast(convert(char(11), getdate(), 113) as datetime)

The second method does send a few more bytes either way but that might not be as important as the speed of the conversion.

第二种方法确实会多发送几个字节,但这可能不如转换速度重要。

Both also appear to be very fast, but there might be a difference in speed when dealing with hundreds-of-thousands or more rows?

两者看起来也都非常快,但是在处理数十万或更多行时速度可能会有所不同?

Also, is it possible that there are even better methods to get rid of the time portion of a datetime in SQL?

另外,是否有更好的方法可以摆脱 SQL 中日期时间的时间部分?

回答by gbn

Strictly, method ais the least resource intensive:

严格来说,方法a是资源最少的:

a) select DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0)

Proven less CPU intensive for same total duration a million rows by some one with way too much time on their hands: Most efficient way in SQL Server to get date from date+time?

证明在相同的总持续时间一百万行的 CPU 密集度较低的人手上有太多的时间:SQL Server 中从日期+时间获取日期的最有效方法?

I saw a similar test elsewhere with similar results too.

我在其他地方也看到过类似的测试,结果也类似。

I prefer the DATEADD/DATEDIFF because:

我更喜欢 DATEADD/DATEDIFF 因为:

Edit, Oct 2011

编辑,2011 年 10 月

For SQL Server 2008+, you can CAST to datei.e. CAST(getdate() AS date). Or just use datedatatype so no time to remove.

对于SQL Server 2008+,你可以同时投射到dateCAST(getdate() AS date)。或者只是使用date数据类型所以没有时间删除。

Edit, Jan 2012

编辑,2012 年 1 月

A worked example of how flexible this is: Need to calculate by rounded time or date figure in sql server

这是多么灵活的一个工作示例:需要在 sql server 中按四舍五入的时间或日期数字计算

Edit, May 2012

编辑,2012 年 5 月

Do not use this in WHERE clauses and the like without thinking: adding a function or CAST to a column invalidates index usage. See number 2 here: http://www.simple-talk.com/sql/t-sql-programming/ten-common-sql-programming-mistakes/

不要在 WHERE 子句等中不加考虑地使用它:向列添加函数或 CAST 会使索引使用无效。请参阅此处的第 2 条:http: //www.simple-talk.com/sql/t-sql-programming/ten-common-sql-programming-mistakes/

Now, this does have an example of later SQL Server optimiser versions managing CAST to date correctly, but generallyit will be a bad idea ...

现在,这确实有一个更高版本的 SQL Server 优化器版本正确管理 CAST 的示例,但通常这将是一个坏主意......

Edit, Sep 2018, for datetime2

编辑,2018 年 9 月,日期时间 2

DECLARE @datetime2value datetime2 = '02180912 11:45' --this is deliberately within datetime2, year 0218
DECLARE @datetime2epoch datetime2 = '19000101'

select DATEADD(dd, DATEDIFF(dd, @datetime2epoch, @datetime2value), @datetime2epoch)

回答by Anto Raja Prakash

In SQL Server 2008, you can use:

在 SQL Server 2008 中,您可以使用:

CONVERT(DATE, getdate(), 101)

回答by Arjan Fraaij

Of-course this is an old thread but to make it complete.

当然,这是一个旧线程,但要使其完整。

From SQL 2008 you can use DATE datatype so you can simply do:

从 SQL 2008 开始,您可以使用 DATE 数据类型,因此您可以简单地执行以下操作:

SELECT CONVERT(DATE,GETDATE())

回答by Gary McGill

SELECT CAST(FLOOR(CAST(getdate() AS FLOAT)) AS DATETIME)

...is nota good solution, per the comments below.

...根据下面的评论,这不是一个好的解决方案。

I would delete this answer, but I'll leave it here as a counter-example since I think the commenters' explanation of whyit's not a good idea is still useful.

我会删除这个答案,但我会把它留在这里作为反例,因为我认为评论者对为什么它不是一个好主意的解释仍然有用。

回答by Metaphor

In SQL Server 2008, there is a DATE datetype (also a TIME datatype).

在 SQL Server 2008 中,有一个 DATE 日期类型(也是一个 TIME 数据类型)。

CAST(GetDate() as DATE)

or

或者

declare @Dt as DATE = GetDate()

回答by Jeff Meatball Yang

Here's yet another answer, from another duplicate question:

这是来自另一个重复问题的另一个答案

SELECT CAST(CAST(getutcdate() - 0.50000004 AS int) AS datetime) 

This magic number method performs slightly faster than the DATEADD method. (It looks like ~10%)

这种幻数方法的执行速度略快于 DATEADD 方法。(看起来像~10%)

The CPU Time on several rounds of a million records:

几轮百万条记录的 CPU 时间:

DATEADD   MAGIC FLOAT
500       453
453       360
375       375
406       360

But note that these numbers are possibly irrelevant because they are already VERY fast. Unless I had record sets of 100,000 or more, I couldn't even get the CPU Time to read above zero.

但请注意,这些数字可能无关紧要,因为它们已经非常快了。除非我有 100,000 或更多的记录集,否则我什至无法让 CPU 时间读数高于零。

Considering the fact that DateAdd is meant for this purpose and is more robust, I'd say use DateAdd.

考虑到 DateAdd 用于此目的并且更健壮,我会说使用 DateAdd。

回答by Byju

SELECT CAST(CAST(GETDATE() AS DATE) AS DATETIME)

回答by emehex

I really like:

我很喜欢:

[date] = CONVERT(VARCHAR(10), GETDATE(), 120)

The 120format code will coerce the date into the ISO 8601 standard:

120格式的代码将迫使日到ISO 8601标准:

'YYYY-MM-DD' or '2017-01-09'

Super easy to use in dplyr (R) and pandas (Python)!

在 dplyr ( R) 和 pandas ( Python) 中超级好用!

回答by broslav

BEWARE!

谨防!

Method a) and b) does NOT always have the same output!

方法 a) 和 b) 并不总是具有相同的输出!

select DATEADD(dd, DATEDIFF(dd, 0, '2013-12-31 23:59:59.999'), 0)

Output: 2014-01-01 00:00:00.000

输出: 2014-01-01 00:00:00.000

select cast(convert(char(11), '2013-12-31 23:59:59.999', 113) as datetime)

Output: 2013-12-31 00:00:00.000

输出: 2013-12-31 00:00:00.000

(Tested on MS SQL Server 2005 and 2008 R2)

(在 MS SQL Server 2005 和 2008 R2 上测试)

EDIT: According to Adam's comment, this cannot happen if you read the date value from the table, but it can happen if you provide your date value as a literal (example: as a parameter of a stored procedure called via ADO.NET).

编辑:根据 Adam 的评论,如果您从表中读取日期值,则不会发生这种情况,但如果您将日期值作为文字提供(例如:作为通过 ADO.NET 调用的存储过程的参数),则可能会发生这种情况。

回答by Diego

select CONVERT(char(10), GetDate(),126)

选择转换(字符(10),GetDate(),126)