我不确定为什么 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小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
修改现有小数位信息Modify Existing decimal places info(修改现有小数位信息)
多次指定相关名称“CONVERT"The correlation name #39;CONVERT#39; is specified multiple times(多次指定相关名称“CONVERT)
T-SQL 左连接不返回空列T-SQL left join not returning null columns(T-SQL 左连接不返回空列)
从逗号或管道运算符字符串中删除重复项remove duplicates from comma or pipeline operator string(从逗号或管道运算符字符串中删除重复项)
将迭代查询更改为基于关系集的查询Change an iterative query to a relational set-based query(将迭代查询更改为基于关系集的查询)
将零连接到 sql server 选择值仍然显示 4 位而不是concatenate a zero onto sql server select value shows 4 digits still and not 5(将零连接到 sql server 选择值仍然显示 4 位而不是 5)