Dynamic Query Based On Time

Posted on 2007-07-24
Last Modified: 2010-03-20
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?
Question by:sainiak
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    so you are so close ...

    WHERE Timestamp >= dateadd( minute, -datepart(minute, getdate()), dateadd ( hour, -2, getdate() ))
     and Timestamp < dateadd( minute, -datepart(minute, getdate()), dateadd ( hour, -1, getdate() ))

    LVL 142

    Accepted Solution

    ok, that was only setting the minute to 0...
    here to also set the second to 0:

    WHERE Timestamp >= dateadd( second, -datepart(second, getdate()), dateadd( minute, -datepart(minute, getdate()), dateadd ( hour, -2, getdate() )))
     and Timestamp < dateadd( second, -datepart(second, getdate()), dateadd( minute, -datepart(minute, getdate()), dateadd ( hour, -1, getdate() )))

    LVL 92

    Expert Comment

    by:Patrick Matthews
    WHERE Timestamp >= DATEADD(hh, DATEPART(hh, GETDATE()) - 1, CONVERT(datetime, CONVERT(varchar, GETDATE(), 102)) AND
        TimeStamp < DATEADD(hh, DATEPART(hh, GETDATE()), CONVERT(datetime, CONVERT(varchar, GETDATE(), 102))

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now