Records for last 2 hours

  • Out Table has date as '2014-04-25' and time as 12:00 AM , 1:01 Am ,12:10 pm etc....

    I need to see records for last two hours ...

    How should I do so?

  • With the little data you have posted ... use Books on Line and check the use of the BETWEEN statement

    From Books on Line:

    test_expression BETWEEN begin_expression AND end_expression

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • You will need to use DATEADD as well.

    http://technet.microsoft.com/en-us/library/ms186819.aspx

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • sharonsql2013 (3/25/2014)


    Out Table has date as '2014-04-25' and time as 12:00 AM , 1:01 Am ,12:10 pm etc....

    I need to see records for last two hours ...

    How should I do so?

    select

    *

    from

    YourTable

    where

    datecolumn between dateadd(hour,-2,getdate()) and getdate();

  • sharonsql2013 (3/25/2014)


    Out Table has date as '2014-04-25' and time as 12:00 AM , 1:01 Am ,12:10 pm etc....

    I need to see records for last two hours ...

    How should I do so?

    Does this mean you have two columns?

    What is the data type of the column(s)?

  • Let me be a little bit more clear on that...

    There are two columns

    CurrentDate Date,

    Time Varchar(10)

    If I do , Select * from Data

    Order By Date Desc , Time Desc shows the following results

    The result is :

    2014-03-26 3:01 AM

    2014-03-26 2:01 PM

    2014-03-26 2:01 AM

    2014-03-26 12:01 PM

    2014-03-26 12:01 AM

    2014-03-26 11:01 AM *********** Shouldn't this be on top of :01 am?

    I need to see the latest Date and Time in an order... If I resolve this I will be able to resolve my issue.

  • You can combine the two thus

    SELECT CAST(CurrentDate AS DATETIME) + CAST(Time AS DATETIME) FROM Data

    This will be a DATETIME value. So you might use a CTE to combine the values then do the compare

  • Thanks . That helps.

  • If you would like some real help solving your problem please read the following article. It will show you what you need to post and how to post it to get the best possible responses to your question. Remember, we can't see what you see so you need to help us help you.

    http://www.sqlservercentral.com/articles/Best+Practices/61537/

Viewing 9 posts - 1 through 8 (of 8 total)

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