Home » Other » General » Puzzle n°07 - Create A Calendar for the Given Month and Year *
Puzzle n°07 - Create A Calendar for the Given Month and Year * [message #290970] Wed, 02 January 2008 07:11 Go to next message
rajavu1
Messages: 1075
Registered: May 2005
Location: Bangalore
Senior Member

This might be Old and 'Outdated' version of Puzzle but might be enough to rise some sort of curiosity.

This puzles is about to create a calendar representation of given month in given year in rows and columns like


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 !!!

[Please add this to sticky Puzzle , if it is found interesting ]

Thumbs Up
Rajuvan.

[Edit MC: change Z^001 to number and add complexity rate, please Rajuvan do it yourself next time]


[Updated on: Wed, 02 January 2008 08:57] by Moderator

Re: Puzzle n°07 - Create A Calendar for the Given Month and Year * [message #297166 is a reply to message #290970 ] Wed, 30 January 2008 07:32 Go to previous message
Michel Cadot
Messages: 17653
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
As there are many ways to do it and already published in PL/SQL, I will provide a new one fully in SQL.
SQL> Def month=11
SQL> Def year=1956
SQL> alter session set nls_territory=AMERICA nls_date_language=AMERICAN;
SQL> Set feed off
SQL> Set head off
SQL> Def month=11
SQL> Def year=1956
SQL> Col line format a50
SQL> Col nop noprint
SQL> With 
  2    -- days: 1 line per week day
  3    days as ( select level day from dual connect by level <= 7 ),
  4    -- weeks: 1 line per possible week in a month
  5    weeks as ( select level-1 week from dual connect by level <= 6 ),
  6    -- mdays: each day of the month within each week
  7    mdays as ( 
  8      select week, weekday,
  9             case 
 10               when day > to_char(last_day(to_date('&Month/&Year','MM/YYYY')),'DD')
 11                 then '   '
 12               when day <= 0 then '   '
 13               else to_char(day,'99')
 14             end monthday
 15      from ( select week, day weekday,
 16                    7*week+day-to_char(to_date('&Month/&Year','MM/YYYY'),'D')+1 day
 17             from weeks, days
 18           )
 19    )
 20  -- Display blank line
 21  select 0 nop, null line from dual
 22  union all
 23  -- Display Month title
 24  select 1 nop,
 25         to_char(to_date('&month/&year','MM/YYYY'),' FMMonth YYYY') line
 26  from dual
 27  union all
 28  -- Display blank line
 29  select 2 nop, null line from dual
 30  union all
 31  -- Display week day name
 32  select 3 nop, 
 33         sys_connect_by_path(substr(to_char(trunc(sysdate,'D')+day-1,'Day'),
 34                                    1,3),' ') line
 35  from days
 36  where day = 7
 37  connect by prior day = day-1
 38  start with day = 1
 39  union all
 40  -- Display each week
 41  select 4+week nop, replace(sys_connect_by_path(monthday,'/'), '/', ' ') line
 42  from mdays
 43  where weekday = 7
 44  connect by prior week = week and prior weekday = weekday-1
 45  start with weekday = 1
 46  order by 1
 47  /

 November 1956

 Sun Mon Tue Wed Thu Fri Sat
                   1   2   3
   4   5   6   7   8   9  10
  11  12  13  14  15  16  17
  18  19  20  21  22  23  24
  25  26  27  28  29  30

What is interesting is that whatever is your country, the query always displays the calendar with the first day of week for you in the first column (Lundi is Monday in French):
SQL> alter session set nls_territory=FRANCE nls_date_language=FRENCH;
47  /

Novembre 1956

Lun Mar Mer Jeu Ven Sam Dim
              1   2   3   4
  5   6   7   8   9  10  11
 12  13  14  15  16  17  18
 19  20  21  22  23  24  25
 26  27  28  29  30

SQL> alter session set nls_territory=IRAQ nls_date_language=AMERICAN;
 47  /

 November 1956

 Sat Sun Mon Tue Wed Thu Fri
                       1   2
   3   4   5   6   7   8   9
  10  11  12  13  14  15  16
  17  18  19  20  21  22  23
  24  25  26  27  28  29  30

Regards
Michel
Previous Topic:Oracle XE problem wit polish chars
Next Topic:LDAP Metrics10g, V 10.1.2, Solaris 5.9
Goto Forum:
  


Current Time: Thu Jul 24 15:24:14 CDT 2008

Total time taken to generate the page: 0.02702 seconds
.:: Forum Home :: Site Home :: Wiki Home :: Contact :: Privacy ::.