SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query of set time range relative to current date
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

nathan323
Starting Member

Australia
2 Posts

Posted - 12/26/2013 :  16:32:15  Show Profile  Reply with Quote
Hi. I come in to work 6.30am, and need to audit what happened from 5pm when I left to 6.30am this morning. I have used code to search 13.5 hours back from any given time:
SELECT * FROM TRANSACTION_HISTORY
WHERE TRANSACTION_HISTORY.ACTIVITY_DATE_TIME > (SELECT DATEADD(hour,-13.5,(SELECT MAX (TRANSACTION_HISTORY.ACTIVITY_DATE_TIME) FROM TRANSACTION_HISTORY)))
Problem is if I run query later, I lose time from the start, eg. If I run query at 7am, I only get results from 5.30pm onwards. Rather than change criteria every day, I wanted to able to search from 6.30am of the current day, back to 5.30pm of the previous day. Can this be done?

nathan323
Starting Member

Australia
2 Posts

Posted - 12/26/2013 :  19:42:49  Show Profile  Reply with Quote
Got it.

SELECT * FROM TRANSACTION_HISTORY th
WHERE th.ACTIVITY_DATE_TIME between cast(cast(getdate() -1 as date) as datetime) + 17.5/24.0 AND cast(cast(getdate() as date) as datetime) + 6.5/24.0;
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
17681 Posts

Posted - 12/27/2013 :  00:55:40  Show Profile  Reply with Quote

WHERE th.ACTIVITY_DATE_TIME >= dateadd(day, datediff(day, 0, getdate()), -1) + '17:00'
AND   th.ACTIVITY_DATE_TIME <  dateadd(day, datediff(day, 0, getdate()),  0) + '06:30'



KH
Time is always against us

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 12/28/2013 :  04:44:03  Show Profile  Reply with Quote
you may just collapse it to this


WHERE th.ACTIVITY_DATE_TIME >= dateadd(day, datediff(day, 1, getdate()), '17:00')
AND   th.ACTIVITY_DATE_TIME <  dateadd(day, datediff(day, 0, getdate()),  '06:30') 


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000