How to query based on a specific time range...

  • Can anyone tell me how to query data between specific time range.

    For example: I would like to get all the rows who's ActionTimeStamp between 7:30am and 8:30am.

    Any suggestions would be greatly appreciated.

    SELECT * FROM Log

    WHERE CONVERT(VARCHAR(10), ActionTimeStamp, 101) = CONVERT(VARCHAR(10), '02/10/2012', 101)

    AND ((DATEPART(HH,ActionTimestamp) >= 7 AND DATEPART(MI,ActionTimestamp) >= 30)

    AND (DATEPART(HH,ActionTimestamp) <= 8 AND DATEPART(MI,ActionTimestamp) <= 30))

    This is the query i use but I do not get all the rows back. I get only the rows that has time stamp of 8:30.

    BTW the ActionTimestamp datatype is DATETIME

    Note: This time range is selected by the user as the criteria, hence its dynamic. The query I put in here is to try just as an example but eventually it will be dynamic and nothing can be hardcoded. 🙂

  • SELECT *

    FROM Log

    WHERE ActionTimeStamp >= '2012-10-02 07:30:00' AND ActionTimeStamp <= '2012-10-02 08:30:00'


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Thank you Cadavre for your quick response. But this returns zero rows. Also, I cannot hardcode values and it should be dynamic based on the user selected criteria.

  • Would need to use parameters and ensure that your passing an ISO date format through to the query, something like the below, so the user can pass in a start and end date.

    DELCARE @StartDate DATETIME, @EndDate DATETIME

    SET @StartDate = '2012-02-10 07:30'

    SET @EndDate = '2012-02-10 08:30'

    SELECT

    *

    FROM

    Log

    WHERE

    ActionTimeStamp BETWEEN @StartDate AND @EndDate

  • Please will you post some sample data and expected results based on that data? I'm struggling to understand whether you want everything between 7:30 and 8:30 on a particular date, or on any date. If it's the latter, you may consider converting to the time data type.

    John

  • Vincy-856784 (2/13/2012)


    Thank you Cadavre for your quick response. But this returns zero rows. Also, I cannot hardcode values and it should be dynamic based on the user selected criteria.

    If my query returned 0, then there is no data matching the criteria that you stated.

    The "hardcode" values was for demonstration, you'd use parameters from your app as anthony shows in his post. The point I was trying to get across was that you should use DATETIME data as DATETIME data. By wrapping it in functions as you did, you just confuse the query optimiser and make it take longer (it can't use any indexes that you may have on the DATETIME data in an efficient way).


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Thank you Anthony! Your solution worked perfectly. I had to create the ISO date string and then use between operator to make work perfectly. For some reason the >= and/or <= doesn't work. 🙁

    Thank you all for your quick responses.

    For those who're looking for the solution like me the exact query is as shown below:

    SELECT * FROM Log

    WHERE CONVERT(VARCHAR(30),ActionTimestamp, 20) BETWEEN '2012-02-10 08:00' AND '2012-02-10 08:30'

  • why are you converting the datetime value to a varchar and then comparing it to a datetime? leave the data types alone and use formatted ISO date formats to ensure data consistancy.

  • I tried first but gave me conversion error. But now, it works without converting to string. Thank you Anthony one more time. You saved my day! 🙂

    SELECT * FROM Log WHERE ActionTimestamp BETWEEN '2012-02-10 08:00' AND '2012-02-10 08:30'

  • Vincy-856784 (2/13/2012)


    I tried first but gave me conversion error. But now, it works without converting to string. Thank you Anthony one more time. You saved my day! 🙂

    SELECT * FROM Log WHERE ActionTimestamp BETWEEN '2012-02-10 08:00' AND '2012-02-10 08:30'

    Heh... not taking a thing away from Anthony but it's amazing that we've come full circle here. 😉

    Cadavre (2/13/2012)


    SELECT *

    FROM Log

    WHERE ActionTimeStamp >= '2012-10-02 07:30:00' AND ActionTimeStamp <= '2012-10-02 08:30:00'

    Unless something is drastically wrong with the datatypes, both should work equally well.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • P.S. I won't explain it now because you won't believe me but when you eventually get burned by using BETWEEN with datetime datatypes, remember this post and that I'm telling you it's a terrible habit to get into. 😉

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply