在sql中从当前时间获取过去24小时

时间:2023-02-06
本文介绍了在sql中从当前时间获取过去24小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我不确定为什么 dateadd 函数在这里不起作用.我试图仅从当前时间拉出最后 24 小时,但我看到的时间是今天日期的下午 3 点到 6 点.数据类型是日期时间,但我不确定这里发生了什么.

I am not sure why the dateadd function is not working here. I am trying to pull only the last 24 hours from current time but i see hours like 3-6 pm of today's date. the data type is datetime but i am not sure what is going on here.

select Name, location, myDate from myTable where myDate >= DATEADD(hh, -24, GETDATE())

当我运行上面的查询时,结果将包括:

when i run the query above the outcome will include this:

2015-03-05 15:00:00.000
2015-03-05 15:30:00.000
2015-03-05 16:00:00.000
2015-03-05 16:30:00.000
2015-03-05 17:00:00.000
2015-03-05 17:30:00.000
2015-03-05 18:00:00.000
2015-03-05 18:30:00.000
2015-03-05 19:00:00.000
2015-03-05 19:30:00.000
2015-03-05 20:00:00.000
2015-03-05 20:30:00.000
2015-03-05 21:00:00.000
2015-03-05 21:30:00.000
2015-03-05 22:00:00.000
2015-03-05 22:30:00.000
2015-03-05 23:00:00.000
2015-03-05 23:30:00.000

我原以为根本看不到这些时间.

I was expecting not to see these hours at all.

推荐答案

Use BETWEEN, 即

Use BETWEEN, ie

select Name, location, myDate from myTable where myDate between DATEADD(hh, -24, GETDATE()) and GETDATE()

myDate >= DATEADD(hh, -24, GETDATE()) 获取 myDate 大于 24 小时前的所有记录,包括具有未来日期的记录(如果它们是正确的)有未来的日期是另一回事......)

This myDate >= DATEADD(hh, -24, GETDATE()) gets you all records where myDate is greater than 24 hours ago, including records that have future dates(if they are correct to have future dates is another story...)

这篇关于在sql中从当前时间获取过去24小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:T-SQL 返回表的所有三向组合 下一篇:SQL Server - 针对两列中的重复值进行反向验证 [内部示例]

相关文章

最新文章