Date Manipulation

  • I'm trying to take the date from one field, minus a year from it and then add a day to it.

    Example: Anniversary_Date = 5/31/2009

    I want to subtract a year and add a day and store that value in another field. End result should be 6/1/2008

    I can't figure out how to combine the dateadd feature into one statement.

    Update Name

    set bill_begin = DATEADD ( year , -1, anniversary_date ) (this takes care of the year... can I manipulate the day as well in the same statement?)

    Thanks

    Roger

  • Try this:

    DECLARE @date DATETIME

    SELECT @date = '5/31/2009'

    SELECT @date = DATEADD(year, -1, @date) + 1

    SELECT @date AS date

    /*

    date

    -----------------------

    2008-06-01 00:00:00.000

    */

    .

  • Thanks! I get trying to use the DATEADD function again in the same line.

    Roger

  • Roger Abram (9/27/2008)


    Thanks! I get trying to use the DATEADD function again in the same line.

    Roger

    ... and that will work, also... although it's a bit uglier...

    DECLARE @date DATETIME

    SELECT @date = '5/31/2009'

    SELECT @date = DATEADD(day,1,DATEADD(year, -1, @date))

    SELECT @date AS date

    --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)
    Intro to Tally Tables and Functions

Viewing 4 posts - 1 through 3 (of 3 total)

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