1. Get rid of all advertisements and get unlimited access to documents by upgrading to Premium Membership. Upgrade to Premium Now and also get a Premium Badge!

Generate a Calendar for a particular month ( Puzzle )

Discussion in 'General' started by rajavu, Oct 20, 2008.

  1. rajavu

    rajavu Forum Guru

    Messages:
    815
    Likes Received:
    52
    Trophy Points:
    610
    Location:
    @ Bangalore , India
    This puzles is about to create a calendar representation of given month in given year ( either in SQL or PL/SQL ) in rows and columns like


    Code (Text):

    SQL> /

    SU MO TU WE TH FR SA
    -- -- -- -- -- -- --
    -- -- 01 02 03 04 05
    06 07 08 09 10 11 12
    13 14 15 16 17 18 19
    20 21 22 23 24 25 26
    27 28 29 30 31 -- --

     
    In case Input is '01' for month and '2008' for year .

    Have a nice Luck !!!
     
  2. Arju

    Arju Forum Expert

    Messages:
    107
    Likes Received:
    5
    Trophy Points:
    240
    Location:
    Bangladesh
  3. Sadik

    Sadik Community Moderator Forum Guru

    Messages:
    1,906
    Likes Received:
    252
    Trophy Points:
    1,455
    Good puzzle rajavu, ok i will try this one out tonight myself in a different way of possible.

    and good reference link Arju.. don't you want to try out?
     
  4. Sadik

    Sadik Community Moderator Forum Guru

    Messages:
    1,906
    Likes Received:
    252
    Trophy Points:
    1,455
    Hi.. this is not perfect but have a look...

    Code (Text):

    SQL*Plus: Release 10.2.0.1.0 - Production on Mon Oct 20 19:28:07 2008

    Copyright (c) 1982, 2005, Oracle.  All rights reserved.


    Connected to:
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
    With the Partitioning, OLAP and Data Mining options

    SQL> set serverout on
    SQL> set verify off
    SQL> define my_month = '01-JAN-08'
    SQL> DECLARE
      2     c   NUMBER := TO_NUMBER (TO_CHAR (TRUNC (TO_DATE ('&my_month'), 'MM'), 'D'));
      3  BEGIN
      4     DBMS_OUTPUT.put_line ('|Sun Mon Tue Wed Thu Fri Sat');
      5     DBMS_OUTPUT.put ('|' || RPAD (' ', (c - 1) * 4, ' '));
      6  
      7     FOR i IN 1 .. TO_NUMBER (TO_CHAR (LAST_DAY (TO_DATE ('&my_month')), 'DD'))
      8     LOOP
      9        IF MOD (c + i - 2, 7) = 0
     10        THEN
     11           DBMS_OUTPUT.new_line;
     12           DBMS_OUTPUT.put ('|');
     13        END IF;
     14  
     15        DBMS_OUTPUT.put (' ' || TO_CHAR (i, 'fm00') || ' ');
     16     END LOOP;
     17  
     18     DBMS_OUTPUT.new_line;
     19  END;
     20  /
    |Sun Mon Tue Wed Thu Fri Sat
    |         01  02  03  04  05
    | 06  07  08  09  10  11  12
    | 13  14  15  16  17  18  19
    | 20  21  22  23  24  25  26
    | 27  28  29  30  31

    PL/SQL procedure successfully completed.