Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2004
    Posts
    10

    Unanswered: Months and Years between Dates

    Hi All,
    I am stuck with a Date problem which I am trying to execute in a Stored Proc.

    I basically get a Start Month and a Start Year And an End month & an End Year from a screen that I build.
    Now what I want to do is to traverse all the Months_Year for this period.

    ie For example if my start year is Feb 2001 and end Year is July 2004, then I need
    Feb 2001
    March 2001
    April 2001
    .....
    Jan 2004
    ..
    July 2004 in a cursor.


    Thanks in anticipation.
    Raman.

  2. #2
    Join Date
    Nov 2003
    Posts
    65

    Re: Months and Years between Dates

    Hmmm what database are you using Oracle or Sybase ?
    if its Sybase you can use the datediff to get your dates
    if in Oracle you can use the to_date, to_char to manipulate the given dates and return what you want.


    Originally posted by ramanjaiya
    Hi All,
    I am stuck with a Date problem which I am trying to execute in a Stored Proc.

    I basically get a Start Month and a Start Year And an End month & an End Year from a screen that I build.
    Now what I want to do is to traverse all the Months_Year for this period.

    ie For example if my start year is Feb 2001 and end Year is July 2004, then I need
    Feb 2001
    March 2001
    April 2001
    .....
    Jan 2004
    ..
    July 2004 in a cursor.


    Thanks in anticipation.
    Raman.

  3. #3
    Join Date
    Mar 2004
    Posts
    10

    Re: Months and Years between Dates

    Originally posted by llccoo
    Hmmm what database are you using Oracle or Sybase ?
    if its Sybase you can use the datediff to get your dates
    if in Oracle you can use the to_date, to_char to manipulate the given dates and return what you want.
    I am using SQL Server.
    Basically What I amtrying now is to create 2 Temp tables. One with all the months, and one with all the years & then looping twice to create my combinations.
    I feel it could be done in a better way although!

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    please see the articles The integers table and Finding all the dates between two dates (registration may be required, but it's free)

    the examples show how to generate a series of dates using an integers table

    applied to your example, you would use the integers within a DATEADD() function using the integer as the number of months to add from a starting date
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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