Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2005
    Location
    Brooklyn
    Posts
    25

    Unanswered: get 2 year from the current datetime

    Hello,
    Need help to pass the info below to a variable.
    How can I get 2 year from the current datetime????


    DECLARE @LastOrderDate as smalldatetime

    SET @LastOrderDate = (GetDate() - (2 years))


    Thank you for your help!

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    Code:
    DECLARE @LastOrderDate as smalldatetime
    
    SET @LastOrderDate = DateAdd(year, -2, GetDate()) 
    
    SELECT @LastOrderDate
    -PatP

  3. #3
    Join Date
    Jan 2005
    Location
    Brooklyn
    Posts
    25
    Thank you very much !

  4. #4
    Join Date
    Dec 2008
    Posts
    135
    Quote Originally Posted by Alelena2005
    DECLARE @LastOrderDate as smalldatetime

    SET @LastOrderDate = (GetDate() - (2 years))
    Thank you for your help!
    try like this
    it will works when u given the days only
    not works for years, months
    select getdate() - 731

    if u want the months, years then go for patp solution

  5. #5
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    You shouldn't use that method, because it doesn't work in 2008 and it doesn't work on leap years!

    DateAdd() is the way to go
    George
    Home | Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •