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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Guest UserRank #1,422 · 1.2K reputationPosted Mar 31, 2011, 5:32 AM EDT
Vijay YadavRank #1,637 · 943 reputationPosted Mar 31, 2011, 5:39 AM EDT
Guest UserRank #1,422 · 1.2K reputationPosted Mar 31, 2011, 5:46 AM EDT
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 YadavRank #1,637 · 943 reputationPosted Mar 31, 2011, 5:56 AM EDT
It was a good suggestion, i would use minutes.. :)
Suthish NairRank #92 · 22.8K reputationPosted Mar 31, 2011, 6:13 AM EDT
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')
Guest UserRank #1,422 · 1.2K reputationPosted Mar 31, 2011, 6:18 AM EDT
Vijay YadavRank #1,637 · 943 reputationPosted Mar 31, 2011, 6:20 AM EDT
Guest UserRank #1,422 · 1.2K reputationPosted Mar 31, 2011, 6:25 AM EDT
// 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')
Suthish NairRank #92 · 22.8K reputationPosted Mar 31, 2011, 11:04 AM EDT
Guest UserRank #1,422 · 1.2K reputationPosted Mar 31, 2011, 11:26 AM EDT
Zero is less than 3. ;)
Suthish NairRank #92 · 22.8K reputationPosted Mar 31, 2011, 11:51 AM EDT
Vijay YadavRank #1,637 · 943 reputationPosted Mar 31, 2011, 9:38 PM EDT
It was like a dilemma for me.. anyway :)