Zu Oracle

Monatskalender in SQL*Plus

Bereich:
Versionsinfo:
SQL, PL/SQL
9.2 (Teil 1), 10.2, 11.1, 11.2
Erstellung:
Letzte Überarbeitung:
04/2007 MP
06/2009 MP
 Als PDF Downloaden!

Monatskalender in SQL*Plus


Haben Sie sich auch schon mal gewünscht, dass SQL*Plus einen Monat als schönen Kalender darstellt? Mit der folgenden Funktion können Sie sich den aktuellen Monat (Default) oder einen beliebig anderen Monat anzeigen lassen:

CREATE OR REPLACE FUNCTION show_cal (month IN VARCHAR2 DEFAULT sysdate)
RETURN VARCHAR2 IS
   v_week VARCHAR2(4096);
   v_firstday NUMBER;
   v_lastday NUMBER;
BEGIN
   v_firstday:=to_char(trunc(to_date(month),'MM'),'D');
   v_lastday:=to_number(to_char(last_day(to_date(month)),'DD'));
   v_week:='Monat: '||to_char(to_date(month),'FM Month RRRR')||chr(10);
   v_week:=v_week||'Mon Die Mit Don Fre Sam Son'||chr(10)||
   '-------------------------------------'||chr(10);
   FOR i IN 1.. v_lastday+v_firstday-1 LOOP
      IF i>=v_firstday then
         v_week:=v_week||rpad((i-v_firstday+1),4,' ');
      ELSE
         v_week:=v_week||rpad(chr(32),4,chr(32));
      END IF;
      IF mod(i,7)=0 THEN
         v_week:=v_week||chr(10);
      END IF;
   END LOOP;
   RETURN v_week;
END;
/

show errors

SELECT show_cal FROM dual;

Ausgabe:

SHOW_CAL
---------------------------------------------------
Monat: März 2007
Mon    Die    Mit    Don    Fre    Sam    Son

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



Da viele grafische Tools (Toad, SQL Developer, ...) mit einem Zeilenumbruch nichts anfangen können, hier eine zweite Alternative mit einer Pipelined Function (nur ab Oracle 10g).

CREATE OR REPLACE TYPE calender_week_type
AS OBJECT (woche VARCHAR2(64));
/

CREATE OR REPLACE TYPE calender_type as table of calender_week_type;
/

CREATE OR REPLACE FUNCTION show_cal (month IN VARCHAR2 DEFAULT sysdate)
RETURN calender_type PIPELINED IS
   v_week VARCHAR2(4096);
   v_firstday NUMBER;
   v_lastday NUMBER;
BEGIN
   v_firstday:=to_char(trunc(to_date(month),'MM'),'D');
   v_lastday:=to_number(to_char(last_day(to_date(month)),'DD'));
   PIPE ROW (new calender_week_type('Monat: '||to_char(to_date(month),
                                                  'FM Month RRRR') ));
   PIPE ROW (new calender_week_type('Mon Die Mit Don Fre Sam Son'));
   PIPE ROW (new calender_week_type('--- --- --- --- --- --- ---'));
   FOR i IN 1.. v_lastday+v_firstday-1 LOOP
      IF i>=v_firstday then
         v_week:=v_week||rpad((i-v_firstday+1),4,' ');
      ELSE
         v_week:=v_week||rpad(chr(32),4,chr(32));
      END IF;
      IF mod(i,7)=0 THEN
         PIPE ROW (new calender_week_type(v_week));
         v_week:='';
      END IF;
   END LOOP;
END;
/

show errors

select * from TABLE(CAST(show_cal AS calender_type));


Ausgabe:

Woche
--------------------------------------------------
Monat: März 2007
Mon    Die    Mit    Don    Fre    Sam    Son

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


7 Zeilen ausgewählt.

Suche

Kontakt

Telefon:
  089 6228 6789-0

Telefon (noch gültig):
  089 679090-40

E-Mail Verteiler Monatstipps

Bitte nehmen Sie mich in den Verteiler der monatlichen Tipps & Tricks auf.