在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小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)