Home » Other » General » Puzzle n°07 - Create A Calendar for the Given Month and Year *
|
| 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  |
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
|
|
|
Goto Forum:
Current Time: Thu Jul 24 15:24:14 CDT 2008
Total time taken to generate the page: 0.02702 seconds |