将 SQL 日期时间四舍五入到午夜
我的 SQL 查询遇到一个小问题。我正在使用 GETDATE 函数,但是,假设我在下午 5 点执行脚本,它将提取 2011 年 12 月 12 日下午 5 点到 2011 年 12 月 18 日下午 5 点之间的记录。我怎样才能让它拉出整个12/12/2011 - 12/18/2011的记录基本上忽略时间。
我的脚本:
WHERE Orders.OrderStatus = 'Shipped'
AND Orders.ShipDate > (GETDATE()-6)
I am having a small problem with my SQL query. I'm using the GETDATE function, however, let's say I execute the script at 5PM, it will pull up records between 12/12/2011 5PM to 12/18/2011 5PM. How can I make it pull up records for the whole entire 12/12/2011 - 12/18/2011 basically ignore time.
My script:
WHERE Orders.OrderStatus = 'Shipped'
AND Orders.ShipDate > (GETDATE()-6)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
在 SQL Server 2008 及更高版本中,您可以将
DateTime
转换为Date
,这会删除时间元素。在 SQL Server 2005 及更低版本中,您可以使用:
In SQL Server 2008 and newer you can cast the
DateTime
to aDate
, which removes the time element.In SQL Server 2005 and below you can use:
这是我发现的最简单的事情
DATEDIFF 返回 1900-1-1 之前或之后的整数天数,
并且“转换日期时间”会礼貌地将其恢复到午夜的该日期。
由于 DateDiff 返回一个整数,您可以使用添加或减去天数来获得正确的偏移量。
这不是四舍五入,这是截断......但我认为这就是所问的问题。
(要四舍五入加一并截断...这也不是四舍五入,即上限,但是
再次很可能是你想要的。要真正舍入,请添加 0.5(这有效吗?)并截断。
事实证明,您可以将 .5 添加到 GetDate() 并且它会按预期工作。
我在 SQL Server 2008 上进行了所有试验,但我认为这些功能也适用于 2005。
Here is the simplest thing I've found
The DATEDIFF returns the integer number of days before or since 1900-1-1,
and the Convert Datetime obligingly brings it back to that date at midnight.
Since DateDiff returns an integer you can use add or subtract days to get the right offset.
This isn't rounding this is truncating...But I think that is what is being asked.
(To round add one and truncate...and that's not rounding either, that the ceiling, but
again most likely what you want. To really round add .5 (does that work?) and truncate.
It turns out you can add .5 to GetDate() and it works as expected.
I did all my trials on SQL Server 2008, but I think these functions apply to 2005 as well.
您可以将日期时间转换为日期,然后再转换回日期时间。这将重置时间戳。
You can convert the datetime to a date then back to a datetime. This will reset the timestamp.
结果: 2012-12-14 16:03:33.360
结果 2012-12-15 00:00:00.000
Result: 2012-12-14 16:03:33.360
Result 2012-12-15 00:00:00.000
正如 @BassamMehanni 提到的,您可以在 SQL Server 2008 及以后的版本中将其转换为 DATE...
第二个条件实际上可以只是
GetDate()
,但我将这种格式作为的示例进行展示小于 DateX
以避免将 dateField 也转换为 DATE,从而大幅提高性能。如果您是 2005 年或以下,您可以使用此...
As @BassamMehanni mentioned, you can cast as DATE in SQL Server 2008 onwards...
The second condition can actually be just
GetDate()
, but I'm showing this format as an example ofLess Than DateX
to avoid having to cast the dateField to a DATE as well, thus massively improving performance.If you're on 2005 or under, you can use this...
尝试使用这个。
Try using this.
这可能看起来很便宜,但对我有用
This might look cheap but it's working for me
我通常这样做,
如果你使用的是 SQL SERVER 2008,你可以这样做
希望这有帮助
I usually do
if you are using SQL SERVER 2008, you can do
Hope this helps
你可以把时间四舍五入。
使用下面的
ROUND
会将其四舍五入到午夜。You could round down the time.
Using
ROUND
below will round it down to midnight.您好尝试使用下面的查询
我创建了虚拟数据并测试了上面的查询工作正常。
Hi Try to use below query
I had created dummy data and testested above query works fine.