What definition of week are you trying to implement? ISO?
This is what i use:
REPLACE VIEW sys_calendar.calbasics_iso AS
SELECT
/*** Modify here for different start date ***/
DATE '1900-01-01' AS start_of_calendar,
cdate AS calendar_date,
((cdate - DATE '0001-01-01') MOD 7) + 1 (FORMAT '9') AS day_of_week,
EXTRACT(DAY FROM cdate) (FORMAT '99') AS day_of_month,
cdate - ((EXTRACT(YEAR FROM cdate) - 1900) * 10000 + 0101 (DATE)) + 1 (FORMAT '9999') AS day_of_year,
cdate - start_of_calendar + 1 AS day_of_calendar,
(day_of_month - 1) / 7 + 1 (FORMAT '9') AS WEEKDAY_OF_MONTH,
(day_of_month - day_of_week + 6) / 7 (FORMAT '9') AS week_of_month,
(day_of_calendar - day_of_week + 6) / 7 AS week_of_calendar,
(month_of_year - 1) MOD 3 + 1 AS month_of_quarter,
EXTRACT(MONTH FROM cdate) AS month_of_year,
month_of_year + 12 * (year_of_calendar - EXTRACT(YEAR FROM start_of_calendar)) AS month_of_calendar,
(month_of_year + 2) / 3 AS quarter_of_year,
(month_of_year + 2) / 3 + 4 * (year_of_calendar - EXTRACT(YEAR FROM start_of_calendar)) AS quarter_of_calendar,
EXTRACT(YEAR FROM cdate) AS year_of_calendar,
CASE day_of_week
WHEN 1 THEN 'Monday'
WHEN 2 THEN 'Tuesday'
WHEN 3 THEN 'Wednesday'
WHEN 4 THEN 'Thursday'
WHEN 5 THEN 'Friday'
WHEN 6 THEN 'Saturday'
WHEN 7 THEN 'Sunday'
ELSE ''
END AS weekday,
/**ISO_temp: this week's thursday**/
cDate - Day_Of_Week + 4 AS ISO_temp,
EXTRACT (YEAR FROM ISO_temp) (FORMAT '9999') AS ISO_year,
((ISO_temp - ((EXTRACT(YEAR FROM ISO_temp) - 1900) * 10000 + 0101 (DATE))) / 7) + 1 (FORMAT '99')
AS ISO_week,
ISO_year || 'W' || ISO_week AS Week_ISO
FROM sys_calendar.caldates
;
REPLACE VIEW sys_calendar.ISO_calendar AS
SELECT
calendar_date,
day_of_week,
day_of_month,
day_of_year,
day_of_calendar,
WEEKDAY_OF_MONTH,
week_of_month,
week_of_calendar,
ISO_Week,
month_of_quarter,
month_of_year,
month_of_calendar,
year_of_calendar,
quarter_of_year,
quarter_of_calendar
FROM sys_calendar.calbasics_iso
;Btw, in TD14 there's a new ISO calendar built-in...
Dieter
↧