SQL Server 中的 Trunc(sysdate)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1713127/
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
Trunc(sysdate) in SQL Server
提问by Domnic
What is the equivalent of:
相当于:
TRUNC(SYSDATE)
...in SQL Server 2005?
...在 SQL Server 2005 中?
回答by OMG Ponies
Recommended:
受到推崇的:
DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
This is another alternative, but it's risky because of casting to a FLOAT. It's also been demonstrated to not scale performance as well as the DATEADD/DATEDIFF approach.
这是另一种选择,但由于强制转换为 FLOAT,因此存在风险。它还被证明无法像 DATEADD/DATEDIFF 方法那样扩展性能。
CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME)
回答by stoft
Another option is to use CONVERT(MSSQL 2008 and later) and either use an appropriate style or use a style that you can then SUBSTRING. I have no idea about the performance compared to the dateadd/datediff solution though.
另一种选择是使用CONVERT(MSSQL 2008 及更高版本),并使用适当的样式或使用随后可以 SUBSTRING 的样式。我不知道与 dateadd/datediff 解决方案相比的性能。
e.g.
例如
SELECT SUBSTRING(CONVERT(nvarchar(30), GETDATE(), 120), 1, 16)
SELECT SUBSTRING(CONVERT(nvarchar(30), GETDATE(), 120), 1, 16)
Returns:
返回:
2012-01-03 15:30
2012-01-03 15:30
Example using group that lists rows created per minute (presupposes a 'created' datetime column):
使用组的示例列出每分钟创建的行(假定为“创建”日期时间列):
SELECT SUBSTRING(CONVERT(nvarchar(30), created, 120), 1, 16) as [minute]
, COUNT(1) as [per min]
FROM foo
GROUP BY SUBSTRING(CONVERT(nvarchar(30), created, 120), 1, 16)