Calendar Generator SQL
Calendar Generator SQL
Calendar Generator SQL
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:
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