Home » Other » Training & Certification » To display cosecutive 50 month dates using For loop
icon9.gif  To display cosecutive 50 month dates using For loop [message #266676] Tue, 11 September 2007 06:57 Go to next message
pallavi20
Messages: 5
Registered: September 2007
Junior Member
Hi,

I want to display 50 consecutive months(2007 to 2010)using the dbms_output.put_line command inside for - loop.

Thanks
Re: To display cosecutive 50 month dates using For loop [message #266678 is a reply to message #266676] Tue, 11 September 2007 06:58 Go to previous messageGo to next message
MarcL
Messages: 455
Registered: November 2006
Location: Connecticut, USA
Senior Member
Sounds like homework to me.

What have you tried so far? Show what you have done and what you don't understand.
Re: To display cosecutive 50 month dates using For loop [message #266681 is a reply to message #266676] Tue, 11 September 2007 07:02 Go to previous messageGo to next message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Search for calendar.

Regards
Michel
Re: To display cosecutive 50 month dates using For loop [message #266685 is a reply to message #266678] Tue, 11 September 2007 07:19 Go to previous messageGo to next message
pallavi20
Messages: 5
Registered: September 2007
Junior Member
I have never worked on oracle using for loops,can you please post an example.
Re: To display cosecutive 50 month dates using For loop [message #266690 is a reply to message #266685] Tue, 11 September 2007 07:31 Go to previous messageGo to next message
Soumen Kamilya
Messages: 128
Registered: August 2007
Location: Kolkata
Senior Member

I think you are trying to retrive the following thing:
begin
for c1 in (select level,to_char(add_months(to_date('Jan-'||'2007','Mon-yyyy'),level-1),'Mon-YYYY') as months
from dual
connect by level <=50)
loop
dbms_output.put_line('Month No : '||lpad(c1.level,2,'0')||' is : '||c1.months);
end loop;
end;


output is :
Month No : 01 is : Jan-2007
Month No : 02 is : Feb-2007
Month No : 03 is : Mar-2007
Month No : 04 is : Apr-2007
Month No : 05 is : May-2007
Month No : 06 is : Jun-2007
Month No : 07 is : Jul-2007
Month No : 08 is : Aug-2007
Month No : 09 is : Sep-2007
Month No : 10 is : Oct-2007
Month No : 11 is : Nov-2007
Month No : 12 is : Dec-2007
Month No : 13 is : Jan-2008
Month No : 14 is : Feb-2008
Month No : 15 is : Mar-2008
Month No : 16 is : Apr-2008
Month No : 17 is : May-2008
Month No : 18 is : Jun-2008
Month No : 19 is : Jul-2008
Month No : 20 is : Aug-2008
Month No : 21 is : Sep-2008
Month No : 22 is : Oct-2008
Month No : 23 is : Nov-2008
Month No : 24 is : Dec-2008
Month No : 25 is : Jan-2009
Month No : 26 is : Feb-2009
Month No : 27 is : Mar-2009
Month No : 28 is : Apr-2009
Month No : 29 is : May-2009
Month No : 30 is : Jun-2009
Month No : 31 is : Jul-2009
Month No : 32 is : Aug-2009
Month No : 33 is : Sep-2009
Month No : 34 is : Oct-2009
Month No : 35 is : Nov-2009
Month No : 36 is : Dec-2009
Month No : 37 is : Jan-2010
Month No : 38 is : Feb-2010
Month No : 39 is : Mar-2010
Month No : 40 is : Apr-2010
Month No : 41 is : May-2010
Month No : 42 is : Jun-2010
Month No : 43 is : Jul-2010
Month No : 44 is : Aug-2010
Month No : 45 is : Sep-2010
Month No : 46 is : Oct-2010
Month No : 47 is : Nov-2010
Month No : 48 is : Dec-2010
Month No : 49 is : Jan-2011
Month No : 50 is : Feb-2011


Cheers
Soumen
Re: To display cosecutive 50 month dates using For loop [message #266703 is a reply to message #266690] Tue, 11 September 2007 08:08 Go to previous messageGo to next message
Soumen Kamilya
Messages: 128
Registered: August 2007
Location: Kolkata
Senior Member

May be you need this code:
DECLARE
  Counter  NUMBER;
BEGIN
	DBMS_OUTPUT.PUT_LINE('WELCOME TO MY CALENDER!');
  FOR c1 IN (SELECT LEVEL,
                    Add_months(To_date('01/01/'
                                       ||'2007','dd/mm/yyyy'),LEVEL - 1) AS Months
             FROM   Dual
             CONNECT BY LEVEL <= 50)
  LOOP
    DECLARE
      CURSOR c2(Months IN DATE) IS
        SELECT LEVEL
        FROM   Dual
        CONNECT BY LEVEL <= To_char(Last_day(Months),'dd');
       CURSOR c4(Dates IN DATE) IS
         SELECT To_char(To_date('01/'
                                ||To_char(Dates,'mm/yyyy'),'dd/mm/yyyy') + LEVEL - 1,
                        'Dy') AS Days
         FROM   Dual
         CONNECT BY LEVEL <= 7;
    BEGIN
      dbms_Output.Put_Line(NULL);


dbms_Output.Put_Line('Month No : '||lPad(c1.LEVEL,2,'0')||' is : '||To_Char(c1.Months,'Mon-yyyy'));

      Counter := 0;

      FOR c5 IN c4(c1.Months) LOOP
        dbms_Output.Put(c5.Days
                        ||'	');
      END LOOP;

      dbms_Output.Put_Line(NULL);

      FOR c3 IN c2(c1.Months) LOOP
        Counter := Counter + 1;

		IF mod(Counter,7) = 0 THEN
			dbms_Output.Put_Line(c3.LEVEL);
		ELSE
			dbms_Output.Put(c3.LEVEL||'	');
        END IF;
      END LOOP;
    END;

    dbms_Output.Put_Line(NULL);
  END LOOP;
END;


Cheers
Soumen
Re: To display cosecutive 50 month dates using For loop [message #266714 is a reply to message #266690] Tue, 11 September 2007 08:23 Go to previous messageGo to next message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Hopefully, he did not ask for the 365 of the current year or the about 36525 days of the century.

Regards
Michel
icon6.gif  Re: To display cosecutive 50 month dates using For loop [message #266728 is a reply to message #266703] Tue, 11 September 2007 08:57 Go to previous messageGo to next message
pallavi20
Messages: 5
Registered: September 2007
Junior Member
Thanks Soumen
That was really helpful....
Re: To display cosecutive 50 month dates using For loop [message #266732 is a reply to message #266728] Tue, 11 September 2007 09:03 Go to previous messageGo to next message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
helpful?
Of course, he did your homework for you!

helpful?
Not at all, you didn't learn how to find it by yourself, so you can't answer the next question.

By the way, PL/SQL is not needed for this you can use SQL but do you care?

Regards
Michel
Re: To display cosecutive 50 month dates using For loop [message #267027 is a reply to message #266732] Wed, 12 September 2007 05:08 Go to previous messageGo to next message
pallavi20
Messages: 5
Registered: September 2007
Junior Member
THIS IS A LEARNING STAGE FOR ME ...I TRIED TO FIND AT MANY PLACES BUT MY CODE WAS HAVING ISSUES....I THOUGHT THIS FORUM IS FOR PUTTING UP QUERIES...THATS WHY I ALSO POSTED ONE QUESTION AS IT WAS URGENT....SOME PEOPLE ARE HERE JUST FOR CRITISIZING NOT FOR HELPLING...
Re: To display cosecutive 50 month dates using For loop [message #267029 is a reply to message #267027] Wed, 12 September 2007 05:16 Go to previous messageGo to next message
Frank
Messages: 7901
Registered: March 2000
Senior Member
You seem to have an erroneous picture of the purpose of this site. It is not a requesting place for queries, you can get help here solving troubles you have.
In order to really help you, we like you to show us what you tried and where you are stuck. Providing you with a query, you may not even understand, well, we don't consider that really as helping.
Re: To display cosecutive 50 month dates using For loop [message #267037 is a reply to message #267029] Wed, 12 September 2007 05:52 Go to previous message
pallavi20
Messages: 5
Registered: September 2007
Junior Member
Thanks
going ahead i will keep this in mind...
Previous Topic: completed my OCA 9i
Next Topic: Program to maintain a history of promise dates in dff 2 segments
Goto Forum:
  


Current Time: Fri Mar 29 10:04:02 CDT 2024