to generate all the dates for a given month and year..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jai80
    New Member
    • Nov 2006
    • 30

    to generate all the dates for a given month and year..

    hi frenz,

    I want to display all the dates and day of the given year and month.
    For ex; if i choose 2007 and select the month January, i want to generate all the dates for this selection
    1/1/2007
    2/1/2007
    ....
    ...
    ..
    31/1/2007,
    and the day name (monday,tuesday ...) for all the generated dates. Also, need to check for the leapyear, and generate the dates accordingly. Pls provide some kind-a help. its urgent. I'm able to generate the dates, but need to display the dayname and chk for leap year.

    Cheers,
    jai
  • iburyak
    Recognized Expert Top Contributor
    • Nov 2006
    • 1016

    #2
    Try this:

    [PHP]Declare @year int, @month int, @StartDate datetime, @EndDate datetime
    Declare @Date datetime

    SET @year = 2008
    SET @month = 2
    SET @StartDate = cast(cast(@mont h as varchar(10)) + '-1-' + cast(@year as varchar(10)) as datetime)
    SET @EndDate = dateadd(m,1,@St artDate)

    DECLARE @temp TABLE (Date_Time varchar(50))

    SET @Date = @StartDate

    While @Date < @EndDate
    BEGIN

    INSERT INTO @temp
    SELECT DATENAME (dw, @Date) + ', ' + convert(varchar (30), @Date, 110)

    SELECT @Date = dateadd(d,1, @Date)
    END

    SELECT * from @temp[/PHP]


    Don't worry about leap year it will be handled with internal calendar.

    Good Luck.

    Comment

    • jai80
      New Member
      • Nov 2006
      • 30

      #3
      Hi iburyak,

      Thanks for your help. Sorry for the late reply. i was also working on the code and got the desired output. Once again, thank you.

      Cheers,.
      jai
      Originally posted by iburyak
      Try this:

      [PHP]Declare @year int, @month int, @StartDate datetime, @EndDate datetime
      Declare @Date datetime

      SET @year = 2008
      SET @month = 2
      SET @StartDate = cast(cast(@mont h as varchar(10)) + '-1-' + cast(@year as varchar(10)) as datetime)
      SET @EndDate = dateadd(m,1,@St artDate)

      DECLARE @temp TABLE (Date_Time varchar(50))

      SET @Date = @StartDate

      While @Date < @EndDate
      BEGIN

      INSERT INTO @temp
      SELECT DATENAME (dw, @Date) + ', ' + convert(varchar (30), @Date, 110)

      SELECT @Date = dateadd(d,1, @Date)
      END

      SELECT * from @temp[/PHP]


      Don't worry about leap year it will be handled with internal calendar.

      Good Luck.

      Comment

      Working...