Hi,
I want to get the data of last three hours inserted in the table, i wan to know how i will subtract three hours.
for eg: "select * from table_1 where date_time between CURRENT_TIMESTAMP and ..."Any help?
Hi,
I want to get the data of last three hours inserted in the table, i wan to know how i will subtract three hours.
for eg: "select * from table_1 where date_time between CURRENT_TIMESTAMP and ..."Any help?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Guest UserPosted Mar 31, 2011, 9:32 AM
Vijay YadavPosted Apr 1, 2011, 1:38 AM
It was like a dilemma for me.. anyway :)
Suthish NairPosted Mar 31, 2011, 3:51 PM
Guest UserPosted Mar 31, 2011, 3:26 PM
Zero is less than 3. ;)
Suthish NairPosted Mar 31, 2011, 3:04 PM
Guest UserPosted Mar 31, 2011, 10:25 AM
// returns 1 because a month boundary was crossed
select DATEDIFF(mm, '2011-03-31 12:00:00', '2011-04-01 12:00:00')
// returns 0 because a month boundary was not crossed
select DATEDIFF(mm, '2011-04-01 12:00:00', '2011-04-01 12:00:00')
Vijay YadavPosted Mar 31, 2011, 10:20 AM
Guest UserPosted Mar 31, 2011, 10:18 AM
Suthish NairPosted Mar 31, 2011, 10:13 AM
What happen if this scenario comes, not possible but suppose..
select DATEDIFF(HH, '2011-03-31 12:00:00', '2011-03-31 12:00:00')
Vijay YadavPosted Mar 31, 2011, 9:56 AM
It was a good suggestion, i would use minutes.. :)
Guest UserPosted Mar 31, 2011, 9:46 AM
select DATEDIFF(hh, '2011-03-31 11:59:00', '2011-03-31 12:01:00')
You might expect it to return 0, because there's only a 2 minute difference between those timestamps. Actually it will return 1, because an hour boundary (12:00) was crossed between those two. To make your select more granular, you might want to use minutes instead of hours:
select * from table_1 where DATEDIFF(mi, date_time, GETDATE()) < 180
Vijay YadavPosted Mar 31, 2011, 9:39 AM