Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2003
    Posts
    5

    Unanswered: Beginning PL/SQL question

    I'm writing a pl/sql program to insert the month number and number of days in that month into a table called Month_Days. I only want to use the Insert Into command once, and I also don't want to use a nested If statement. i can get the month number into month column using a loop and a counter going up to 13, but i don't know how to get the number of days into the days column. I'm assuming Jan has 31 days, Feb has 29, March has 31, Apr has 30, etc.
    Here's what i have so far:

    Create Table Month_Days(
    Month Number(2)
    Days Number(2));

    Declare
    LoopX Binary_Integer;
    Begin
    LoopX:=0;
    Loop
    LoopX:=LoopX+1;
    If LoopX=13 Then
    Exit;
    End If;
    Insert Into Month_Days Values (LoopX);
    End Loop;
    End;

    Thanks in advance for any help!

  2. #2
    Join Date
    Nov 2002
    Location
    Desk, slightly south of keyboard
    Posts
    697
    Hi,

    Try this.... change the year or use current year in the TO_DATE line, it will also give 29 days for a leap year if required.

    declare
    nMonth number(2);
    nDays number(2);
    begin
    for nMonth in 1..12 loop
    nDays := TO_CHAR(LAST_DAY(TO_DATE('01/'||nMonth||'/2002','DD/MM/YYYY')),'DD');
    dbms_output.put_line('Month '||nMonth||' days '||nDays);
    end loop;
    end;

    HTH
    Bill

Posting Permissions

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