Spielereien mit dem Datum

Anhand eines Datums die Woche zu bestimmen erfolgt in Oracle über die Formatierung:

select to_char(to_date('31.12.2013','dd.mm.yyyy'),'Dy dd.mm.yyyy') Tag, to_char(to_date('31.12.2013','dd.mm.yyyy'),'yyyy ww') Woche from dual;
select to_char(to_date('31.12.2013','dd.mm.yyyy'),'Dy dd.mm.yyyy') Tag, to_char(to_date('31.12.2013','dd.mm.yyyy'),'yyyy iw') Woche from dual;
select to_char(to_date('31.12.2013','dd.mm.yyyy'),'Dy dd.mm.yyyy') Tag, to_char(to_date('31.12.2013','dd.mm.yyyy'),'iyyy ww') Woche from dual;
select to_char(to_date('31.12.2013','dd.mm.yyyy'),'Dy dd.mm.yyyy') Tag, to_char(to_date('31.12.2013','dd.mm.yyyy'),'iyyy iw') Woche from dual;


Zu Unterscheiden ist dabei zwischen ISO-Jahr /-Woche (IYYY / IW) und "normalem" Jahr / Woche (YYYY / WW).
Entscheidend bei letztem ist die Definition: "Week of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year." -> D.h. der ERSTE Tag des Jahres bestimmt für dieses Jahr den ersten Wochentag bzw. es kann passieren, dass die Wochen von Mittwoch bis Mittwoch gezählt werden.

Dazu folgendes Beispiel:

select to_char(to_date('29.12.2013','dd.mm.yyyy'),'Dy dd.mm.yyyy') Tag, to_char(to_date('29.12.2013','dd.mm.yyyy'),'yyyy ww') Woche, to_char(to_date('29.12.2013','dd.mm.yyyy'),'iyyy iw') Iso_Woche  from dual
union all
select to_char(to_date('30.12.2013','dd.mm.yyyy'),'Dy dd.mm.yyyy') Tag, to_char(to_date('30.12.2013','dd.mm.yyyy'),'yyyy ww') Woche, to_char(to_date('30.12.2013','dd.mm.yyyy'),'iyyy iw') Iso_Woche  from dual
union all
select to_char(to_date('31.12.2013','dd.mm.yyyy'),'Dy dd.mm.yyyy') Tag, to_char(to_date('31.12.2013','dd.mm.yyyy'),'yyyy ww') Woche, to_char(to_date('31.12.2013','dd.mm.yyyy'),'iyyy iw') Iso_Woche  from dual
union all
select to_char(to_date('01.01.2014','dd.mm.yyyy'),'Dy dd.mm.yyyy') Tag, to_char(to_date('01.01.2014','dd.mm.yyyy'),'yyyy ww') Woche, to_char(to_date('01.01.2014','dd.mm.yyyy'),'iyyy iw') Iso_Woche  from dual
union all
select to_char(to_date('02.01.2014','dd.mm.yyyy'),'Dy dd.mm.yyyy') Tag, to_char(to_date('02.01.2014','dd.mm.yyyy'),'yyyy ww') Woche, to_char(to_date('02.01.2014','dd.mm.yyyy'),'iyyy iw') Iso_Woche  from dual
union all
select to_char(to_date('03.01.2014','dd.mm.yyyy'),'Dy dd.mm.yyyy') Tag, to_char(to_date('03.01.2014','dd.mm.yyyy'),'yyyy ww') Woche, to_char(to_date('03.01.2014','dd.mm.yyyy'),'iyyy iw') Iso_Woche  from dual
union all
select to_char(to_date('04.01.2014','dd.mm.yyyy'),'Dy dd.mm.yyyy') Tag, to_char(to_date('04.01.2014','dd.mm.yyyy'),'yyyy ww') Woche, to_char(to_date('04.01.2014','dd.mm.yyyy'),'iyyy iw') Iso_Woche  from dual
union all
select to_char(to_date('05.01.2014','dd.mm.yyyy'),'Dy dd.mm.yyyy') Tag, to_char(to_date('05.01.2014','dd.mm.yyyy'),'yyyy ww') Woche, to_char(to_date('05.01.2014','dd.mm.yyyy'),'iyyy iw') Iso_Woche  from dual
union all
select to_char(to_date('06.01.2014','dd.mm.yyyy'),'Dy dd.mm.yyyy') Tag, to_char(to_date('06.01.2014','dd.mm.yyyy'),'yyyy ww') Woche, to_char(to_date('06.01.2014','dd.mm.yyyy'),'iyyy iw') Iso_Woche  from dual
union all
select to_char(to_date('07.01.2014','dd.mm.yyyy'),'Dy dd.mm.yyyy') Tag, to_char(to_date('07.01.2014','dd.mm.yyyy'),'yyyy ww') Woche, to_char(to_date('07.01.2014','dd.mm.yyyy'),'iyyy iw') Iso_Woche  from dual
union all
select to_char(to_date('08.01.2014','dd.mm.yyyy'),'Dy dd.mm.yyyy') Tag, to_char(to_date('08.01.2014','dd.mm.yyyy'),'yyyy ww') Woche, to_char(to_date('08.01.2014','dd.mm.yyyy'),'iyyy iw') Iso_Woche  from dual;


Ergebnis:
TAG            WOCHE   ISO_WOCHE
-------------- ------- ---------
Sun 29.12.2013 2013 52 2013 52 
Mon 30.12.2013 2013 52 2014 01 
Tue 31.12.2013 2013 53 2014 01 
Wed 01.01.2014 2014 01 2014 01 
Thu 02.01.2014 2014 01 2014 01 
Fri 03.01.2014 2014 01 2014 01 
Sat 04.01.2014 2014 01 2014 01 
Sun 05.01.2014 2014 01 2014 01 
Mon 06.01.2014 2014 01 2014 02 
Tue 07.01.2014 2014 01 2014 02 
Wed 08.01.2014 2014 02 2014 02  


Spannend ist der Wechsel der Wochen. Der 31.12.2013 (Spalte Woche) liegt als einziger Tag in KW 53!

Kommentare

Beliebte Posts aus diesem Blog

PGA unter Oracle 11g

trunc(sysdate) - nette Spiele mit dem Datum

Datapump - Verzeichnis erstellen