Date formula that adds hours and changes date

  • Hi,

    I'm in need of a formula that would account for next day change. I am working on a view that adds 2 hours (due to changing time zone) to the "create date". However, if adding the 2 hours to the create date falls into the next day, the DATE does NOT change. How can I account for that? Im using

    dateadd(hh,2)

    to add the date, but I'm not sure how to account for the next date if the time falls into that.

  • The date does change or am I missing something?

    DECLARE @Date DATETIME = '2019-07-19 22:42:45.880'
    SELECT DATEADD(hh, 2, @Date)

    Result:

    2019-07-20 00:42:45.880

    --Vadim R.

  • uahmed90 wrote:

    Hi, I'm in need of a formula that would account for next day change. I am working on a view that adds 2 hours (due to changing time zone) to the "create date". However, if adding the 2 hours to the create date falls into the next day, the DATE does NOT change. How can I account for that? Im using

    dateadd(hh,2)

    to add the date, but I'm not sure how to account for the next date if the time falls into that.

    One thing that I can say for certain is that the DATEADD function takes 3, not 2, parameters.  See the post that rVadim posted on this thread.

    --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 3 posts - 1 through 2 (of 2 total)

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