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

Getting last 24 hours from current time in sql(在sql中从当前时间获取过去24小时)
本文介绍了在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小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Query with t(n) and multiple cross joins(使用 t(n) 和多个交叉连接进行查询)
Unpacking a binary string with TSQL(使用 TSQL 解包二进制字符串)
Max rows in SQL table where PK is INT 32 when seed starts at max negative value?(当种子以最大负值开始时,SQL 表中的最大行数其中 PK 为 INT 32?)
Inner Join and Group By in SQL with out an aggregate function.(SQL 中的内部连接和分组依据,没有聚合函数.)
Add a default constraint to an existing field with values(向具有值的现有字段添加默认约束)
SQL remove from running total(SQL 从运行总数中删除)