troubleshooting Question

Dynamic Query Based On Time

Avatar of sainiak
sainiakFlag for United States of America asked on
DatabasesMicrosoft SQL Server 2005
3 Comments1 Solution220 ViewsLast Modified:
I have a SQL Server query that runs hourly (not always the same minute) with a dynamic where clause.  I want to retrieve records from the last "complete" hour (minute:second 00:00 to 59:59).

For example, if the query ran at 16:15, I want the records from 15:00:00.000 to 15:59:59.000.

The best I can do is this:
WHERE Timestamp >= dateadd ( hour, -2, getdate() ) and Timestamp < dateadd ( hour, -1, getdate() )
Which returns the records from 14:15 to 15:15.

Is there a way to set the minute, second, and ms = 0 in the WHEREclause, so no matter when it ran in the hour it would return the previous "complete" hour?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros