Hourly

  • I have a list of data with datetime. I would like to pull the data base on hour.

    Example:

    datetime IDSample

    2014-09-23 00:09:14.297153527576

    2014-09-23 00:09:19.903153527579

    2014-09-23 00:09:22.250153527583

    2014-09-23 00:09:23.747153527585

    2014-09-23 08:54:25.393153535349

    2014-09-23 08:54:30.800153535353

    2014-09-23 11:28:35.427153545958

    2014-09-23 11:28:37.203153545961

    2014-09-23 11:28:39.170153545963

    I would like to schedule the job to run every hour.

    Example now is 11:10AM, I only would like to pull data from 11:00 - 11:59

    select * from tableA

    where datetime between 11:10 and 11:59 and datetime=getdate()

    datetime IDSample

    2014-09-23 11:28:35.427153545958

    2014-09-23 11:28:37.203153545961

    2014-09-23 11:28:39.170153545963

    How can I apply on where condition to set extrating every hour.

  • Here you go:-

    SELECT

    SampleDate,

    CAST(SampleDate AS time),

    SampleID

    FROM

    DateTimeQuery

    WHERE

    CAST(SampleDate AS time) >= CAST(GETDATE() AS TIME)

    AND

    CAST(SampleDate AS time) < CAST(DATEADD(hh,1,GETDATE()) AS TIME)

  • how to get 1 hour behind?

    example current timing 11:28, get 10:00 to 11:00

  • SELECT

    SampleDate,

    CAST(SampleDate AS time),

    SampleID

    FROM

    DateTimeQuery

    WHERE

    CAST(SampleDate AS time) >= dateadd(hh, datediff(hh, 0, getdate()) - 1, 0)

    AND

    CAST(SampleDate AS time) < dateadd(hh, datediff(hh, 0, getdate()), 0)

    That will return the data from the start of the previous hour to the start of the current hour


    On two occasions I have been asked, "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" ... I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
    —Charles Babbage, Passages from the Life of a Philosopher

    How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537

  • If rounded interval is needed

    SELECT

    timeStart =

    DATEADD(MINUTE

    ,-DATEPART(MINUTE,getdate())

    ,DATEADD(SECOND

    ,-DATEPART(SECOND,getdate())

    , DATEADD(MS,

    -DATEPART(ms,getdate())

    ,getdate())))

    ,timeEnd =

    DATEADD(MINUTE

    ,-DATEPART(MINUTE,getdate()) + 60

    ,DATEADD(SECOND

    ,-DATEPART(SECOND,getdate())

    , DATEADD(MS,

    -DATEPART(ms,getdate()) - 1

    ,getdate())));

  • I get this error:

    The data types time and datetime are incompatible in the greater than or equal to operator.

  • girl_bj (9/23/2014)


    I get this error:

    The data types time and datetime are incompatible in the greater than or equal to operator.

    Which answer are you getting the error on?


    On two occasions I have been asked, "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" ... I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
    —Charles Babbage, Passages from the Life of a Philosopher

    How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537

  • For the hour behind use:-

    SELECT

    SampleDate,

    CAST(SampleDate AS time),

    SampleID

    FROM

    DateTimeQuery

    WHERE

    CAST(SampleDate AS time) >= CAST(DATEADD(hh,-1,GETDATE()) AS TIME)

    AND

    CAST(SampleDate AS time) < CAST(GETDATE() AS TIME)

    Try that and see if you still get the error

  • serg-52 (9/23/2014)


    If rounded interval is needed

    SELECT

    timeStart =

    DATEADD(MINUTE

    ,-DATEPART(MINUTE,getdate())

    ,DATEADD(SECOND

    ,-DATEPART(SECOND,getdate())

    , DATEADD(MS,

    -DATEPART(ms,getdate())

    ,getdate())))

    ,timeEnd =

    DATEADD(MINUTE

    ,-DATEPART(MINUTE,getdate()) + 60

    ,DATEADD(SECOND

    ,-DATEPART(SECOND,getdate())

    , DATEADD(MS,

    -DATEPART(ms,getdate()) - 1

    ,getdate())));

    Tried this method more accurate.

    Is working fine. Thanks all.

  • How to overcome the end date issue?

    When I try to run it appear together with the minute.

    Example current time 08:49:44.6730000

    StartDate 07:00:00.0000000

    EndDate 08:49:44.6730000

    How to make it

    StartDate 07:00:00.0000000

    EndDate 08:00:00.0000000

    Is it possible?

  • Do you mean how do you show a rounded time in the end date column?


    On two occasions I have been asked, "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" ... I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
    —Charles Babbage, Passages from the Life of a Philosopher

    How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537

  • Yes. Let say the time is 0830.

    Would like to get 0700 to 0800

  • StartDate = dateadd(hh, datediff(hh, 0, '2014-09-24 08:30:00.000') - 1, 0)

    EndDate = dateadd(hh, datediff(hh, 0, '2014-09-24 08:30:00.000'), 0)

    Does that do what you want?


    On two occasions I have been asked, "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" ... I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
    —Charles Babbage, Passages from the Life of a Philosopher

    How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537

  • Im trying to pull it every hour.

    The method you provide need to be done manually?

  • If you change the fixed date to getdate() or the time you ran the report it will round the hour to the start of that hour for the start date and show the start of the hour previously for the end date. Like so:

    select

    StartDate = dateadd(hh, datediff(hh, 0, Getdate()) - 1, 0)

    ,EndDate = dateadd(hh, datediff(hh, 0, getdate()), 0)

    EndDate

    2014-09-24 10:00:00.000

    StartDate

    2014-09-24 09:00:00.000


    On two occasions I have been asked, "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" ... I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
    —Charles Babbage, Passages from the Life of a Philosopher

    How to post a question to get the most help http://www.sqlservercentral.com/articles/Best+Practices/61537

Viewing 15 posts - 1 through 15 (of 17 total)

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