Sei sulla pagina 1di 2

SELECT TO_NUMBER (TO_CHAR (mydate, 'yyyymmdd')) AS date_key,

2 mydate AS date_time_start,
3 mydate + 1 - 1/86400 AS date_time_end,
4 TO_CHAR (mydate, 'dd-MON-yyyy') AS date_value,
5 TO_NUMBER (TO_CHAR (mydate, 'D')) AS day_of_week_number,
6 TO_CHAR (mydate, 'Day') AS day_of_week_desc,
7 TO_CHAR (mydate, 'DY') AS day_of_week_sdesc,
8 CASE WHEN TO_NUMBER (TO_CHAR (mydate, 'D')) IN (1, 7) THEN 1
9 ELSE 0
10 END AS weekend_flag,
11 TO_NUMBER (TO_CHAR (mydate, 'W')) AS week_in_month_number,
12 TO_NUMBER (TO_CHAR (mydate, 'WW')) AS week_in_year_number,
13 TRUNC(mydate, 'w') AS week_start_date,
14 TRUNC(mydate, 'w') + 7 - 1/86400 AS week_end_date,
15 TO_NUMBER (TO_CHAR (mydate, 'IW')) AS iso_week_number,
16 TRUNC(mydate, 'iw') AS iso_week_start_date,
17 TRUNC(mydate, 'iw') + 7 - 1/86400 AS iso_week_end_date,
18 TO_NUMBER (TO_CHAR (mydate, 'DD')) AS day_of_month_number,
19 TO_CHAR (mydate, 'MM') AS month_value,
20 TO_CHAR (mydate, 'Month') AS month_desc,
21 TO_CHAR (mydate, 'MON') AS month_sdesc,
22 TRUNC (mydate, 'mm') AS month_start_date,
23 LAST_DAY (TRUNC (mydate, 'mm')) + 1 - 1/86400 AS month_end_date,
24 TO_NUMBER ( TO_CHAR( LAST_DAY (TRUNC (mydate, 'mm')), 'DD')) AS
days_in_month,
25 CASE WHEN mydate = LAST_DAY (TRUNC (mydate, 'mm')) THEN 1
26 ELSE 0
27 END AS last_day_of_month_flag,
28 TRUNC (mydate) - TRUNC (mydate, 'Q') + 1 AS day_of_quarter_number,
29 TO_CHAR (mydate, 'Q') AS quarter_value,
30 'Q' || TO_CHAR (mydate, 'Q') AS quarter_desc,
31 TRUNC (mydate, 'Q') AS quarter_start_date,
32 ADD_MONTHS (TRUNC (mydate, 'Q'), 3) - 1/86400 AS quarter_end_date,
33 ADD_MONTHS (TRUNC (mydate, 'Q'), 3) - TRUNC (mydate, 'Q') AS
days_in_quarter,
34 CASE WHEN mydate = ADD_MONTHS (TRUNC (mydate, 'Q'), 3) - 1 THEN 1
35 ELSE 0
36 END AS last_day_of_quarter_flag,
37 TO_NUMBER (TO_CHAR (mydate, 'DDD')) AS day_of_year_number,
38 TO_CHAR (mydate, 'yyyy') AS year_value,
39 'YR' || TO_CHAR (mydate, 'yyyy') AS year_desc,
40 'YR' || TO_CHAR (mydate, 'yy') AS year_sdesc,
41 TRUNC (mydate, 'Y') AS year_start_date,
42 ADD_MONTHS (TRUNC (mydate, 'Y'), 12) - 1/86400 AS year_end_date,
43 ADD_MONTHS (TRUNC (mydate, 'Y'), 12) - TRUNC (mydate, 'Y') AS
days_in_year
44 FROM ( SELECT TRUNC (ADD_MONTHS (SYSDATE, -12), 'yy') - 1 + LEVEL AS mydate
45 FROM dual
46 CONNECT BY LEVEL <= (SELECT TRUNC (ADD_MONTHS (SYSDATE, 24), 'yy')
47 - TRUNC (ADD_MONTHS (SYSDATE, -12),
'yy')
48 FROM DUAL
49 )
50 );

The inner query with the CONNECT BY LEVEL syntax returns 1 column with all days
from January 1, 2011 till December 31, 2013. The outer query then uses various
string and date SQL functions to convert the dates into required formats.
Calendar table columns
The above query returns 37 columns 37 various attributes of days. Here are their
data types and short descriptions:

Column Data Type Description


DATE_KEY NUMBER primary key; date in a format YYYYMMDD
DATE_TIME_START DATE beginning of a day (00:00:00 timestamp)
DATE_TIME_END DATE end of a day (23:59:59 timestamp)
DATE_VALUE VARCHAR2(17) string in a format DD-MON-YYYY
DAY_OF_WEEK_NUMBER NUMBER number of a day in a week (from 1 to 7)
DAY_OF_WEEK_DESC VARCHAR2(25) name of a day (Sunday, Monday, ... Saturday)
DAY_OF_WEEK_SDESC VARCHAR2(3) 3-letters abbreviation of a day (SUN, MON, ... SAT)
WEEKEND_FLAG NUMBER flag whether a day is a weekend day (1) or not (0)
WEEK_IN_MONTH_NUMBER NUMBER number of a week in a month (from 1 to 5)
WEEK_IN_YEAR_NUMBER NUMBER number of a week in a year (from 1 to 53)
WEEK_START_DATE DATE date of the first day in a week
WEEK_END_DATE DATE date of the last day in a week
ISO_WEEK_NUMBER NUMBER number of an ISO week in a year (from 1 to 52)
ISO_WEEK_START_DATE DATE date of the first day in an ISO week
ISO_WEEK_END_DATE DATE date of the last day in an ISO week
DAY_OF_MONTH_NUMBER NUMBER number of a day in a month (from 1 to 31)
MONTH_VALUE VARCHAR2(2) 2-digits string in a format MM, number of a month in a year
(from 01 to 12)
MONTH_DESC VARCHAR2(25) full name of a month (January, February, ...
December)
MONTH_SDESC VARCHAR2(3) 3-letters abbreviation of a month (JAN, FEB, ... DEC)
MONTH_START_DATE DATE date of the first day in a month
MONTH_END_DATE DATE date of the last day in a month
DAYS_IN_MONTH NUMBER number of days in a month
LAST_DAY_OF_MONTH_FLAG NUMBER flag whether a day is the last day of a month
(1) or not (0)
DAY_OF_QUARTER_NUMBER NUMBER number of a day in a quarter (from 1 to 92)
QUARTER_VALUE VARCHAR2(1) 1-digit string in a format Q, number of a quarter in
a year (from 1 to 4)
QUARTER_DESC VARCHAR2(2) name of a quarter (Q1, Q2, Q3, Q4)
QUARTER_START_DATE DATE date of the first day in a quarter
QUARTER_END_DATE DATE date of the last day in a quarter
DAYS_IN_QUARTER NUMBER number of days in a quarter
LAST_DAY_OF_QUARTER_FLAG NUMBER flag whether a day is the last day of a
quarter (1) or not (0)
DAY_OF_YEAR_NUMBER NUMBER number of a day in a year (from 1 to 366)
YEAR_VALUE VARCHAR2(4) 4-digits string in a format YYYY (2011, 2012, ...)
YEAR_DESC VARCHAR2(6) another format of a year (YR2011, YR2012, ...)
YEAR_SDESC VARCHAR2(4) short format of a year (YR11, YR12, ...)
YEAR_START_DATE DATE date of the first day in a year
YEAR_END_DATE DATE date of the last day in a year
DAYS_IN_YEAR NUMBER number of days in a year (365 or 366)

The above script is just beginning. A calendar table very often contains also other
types of attributes, such as:

fiscal attributes (fiscal year, fiscal quarter, fiscal month, fiscal week)
holiday flag, business day flag

All such attributes are either company-specific or country-specific. So there is no


general rule that could be used and added into the above query. Once a calendar
table is created and loaded with data, these special attributes need to be handled
afterwards.