問題描述
我正在嘗試提取過去 24 小時內的所有數據,但從當前時間開始.如果當前日期時間是 5/3,時間是 11:30,那么我想從 11:30 拉過去 24 小時.日期字段的數據類型是 datetime
,它只有日期和時間值,沒有秒.這是我當前的查詢
I am trying to pull all data for the last 24 hours but starting from the current time. If the current date-time is 5/3 and the time is 11:30 then i want to pull the last 24 hours from 11:30. The data type for date field is datetime
and it has only the date and time values without the seconds. Here is my current query
select Name, Location, myDate from myTable where myDate>= getdate()-24
上面的查詢給了我一切,但我只想要當前時間.這是表中 myDate 的樣子
the query above is giving me everything but i only want from the current time. this is how myDate look like in the table
2015-03-05 10:30:00.000
2015-03-05 11:00:00.000
2015-03-05 11:30:00.000
2015-03-05 12:00:00.000
2015-03-05 12:30:00.000
2015-03-05 13:00:00.000
2015-03-05 13:30:00.000
2015-03-05 14:00:00.000
2015-03-05 14:30:00.000
推薦答案
為了更明確地表達您的意圖,您可能希望這樣編寫查詢:
To be more explicit with your intentions, you may want to write your query like so:
select Name, Location, myDate from myTable where myDate>= DATEADD(hh, -24, GETDATE())
SQL Server DATEADD
這篇關于如何從當前時間戳獲取過去 24 小時?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!