在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...)
相关文章