Question on DATEADD

  • I have this AND clause:

    AND c.DateCreated BETWEEN DATEADD(YEAR, -5, GETDATE())

    Is this correct? What I am trying to get to is any records from today to 5 years ago.

    Warm Regards,

    Arthur Lorenzini
    Sioux Falls, SD

  • Arthur.Lorenzini (11/10/2008)


    I have this AND clause:

    AND c.DateCreated BETWEEN DATEADD(YEAR, -5, GETDATE())

    Is this correct? What I am trying to get to is any records from today to 5 years ago.

    No, you are missing part of the between and you'll lose records because of the time. I'd change it to this:

    AND c.DateCreated >= dateadd(yy, -5, dateadd(dd, datediff(dd, 0, getdate()), 0) )

    AND c.DateCreated < getdate()

  • well other than the fact that you missing half the BETWEEN statement I would say yes almost there.

    c.Date > = DATEDIFF(yy,-5,GETDATE())

    Please not the this will take into account time as well.

    Thanks

    Chris

    ----------------------------------------------
    Try to learn something about everything and everything about something. - Thomas Henry Huxley

    :w00t:
    Posting Best Practices[/url]
    Numbers / Tally Tables[/url]

    SQL-4-Life
  • Hi Arthur

    It depends, i would use >datadiff 🙂

  • CrazyMan (11/10/2008)


    Hi Arthur

    It depends, i would use >datadiff 🙂

    And would be wrong.

    _____________
    Code for TallyGenerator

  • Christopher Stobbs (11/10/2008)


    well other than the fact that you missing half the BETWEEN statement I would say yes almost there.

    c.Date > = DATEDIFF(yy,-5,GETDATE())

    Please not the this will take into account time as well.

    Thanks

    Chris

    You'd want to use this, and it still has the issue with the time portion:

    c.Date > = DATEADD(yy,-5,GETDATE())

Viewing 6 posts - 1 through 5 (of 5 total)

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