Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2005
    Posts
    92

    Unanswered: Retrieve past 7 years

    Hi All,

    Can someone please suggest me how do I retrieve past 7 years from current year using sql.

    the output year format should be in 'YYYY'

    Thanks
    Pavan

  2. #2
    Join Date
    Aug 2003
    Location
    Where the Surf Meets the Turf @Del Mar, CA
    Posts
    7,776
    Provided Answers: 1
    Code:
      1  select '2010
      2  2009
      3  2008
      4  2007
      5  2006
      6  2005
      7  2004
      8  2003' YEAR
      9* from dual
    SQL> /
    
    YEAR
    ---------------------------------------
    2010
    2009
    2008
    2007
    2006
    2005
    2004
    2003
    You can lead some folks to knowledge, but you can not make them think.
    The average person thinks he's above average!
    For most folks, they don't know, what they don't know.
    Good judgement comes from experience. Experience comes from bad judgement.

  3. #3
    Join Date
    Oct 2005
    Posts
    92

    Retrieve past 7 years

    Thank you for your response.
    However I do not wish to hard code years in the query. suppose if I wish to retrieve past 7 years say from 2011 i will have to change the query If I follow your sugestion.

    Please suggest me if there is any way to accomplish this using sql or pl/sql functions.

    Thanks
    Pavan

  4. #4
    Join Date
    Aug 2003
    Location
    Where the Surf Meets the Turf @Del Mar, CA
    Posts
    7,776
    Provided Answers: 1
    select to_number(extract(year from sysdate))-7 from dual
    You can lead some folks to knowledge, but you can not make them think.
    The average person thinks he's above average!
    For most folks, they don't know, what they don't know.
    Good judgement comes from experience. Experience comes from bad judgement.

  5. #5
    Join Date
    Jun 2004
    Location
    Liverpool, NY USA
    Posts
    2,509
    Code:
    select TO_CHAR(add_months(sysdate,-7*12),'yyyy')+ (ROWNUM) 
    from all_objects
    where rownum < 8
    Bill
    You do not need a parachute to skydive. You only need a parachute to skydive twice.

  6. #6
    Join Date
    Oct 2005
    Posts
    92

    Retrieve past 7 years

    Thank You for your prompt response

Posting Permissions

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