General
=====================================
Code example of creating a table DATES_CALENDER with dates in different format
=====================================
Code Example
=====================================
PROCEDURE populate_dates_calender IS
v_module_name REP_W_LOG.procedure_name%TYPE;
v_msg_text REP_W_LOG.data%TYPE;
v_max_day_str DATES_CALENDER.day%TYPE;
v_max_day DATE;
v_target_day DATES_CALENDER.day%TYPE;
v_dates_calender_row DATES_CALENDER%ROWTYPE;
BEGIN
v_module_name := 'populate_dates_calender';
SELECT MAX(day) into v_max_day_str FROM DATES_CALENDER;
IF v_max_day_str IS NULL THEN
v_max_day_str := '20180101';
END IF;
SELECT TO_CHAR(TO_NUMBER(TO_CHAR(SYSDATE,'YYYY'))+1)||'1231' INTO v_target_day FROM DUAL;
WHILE v_max_day_str < v_target_day LOOP
v_max_day := TO_DATE(v_max_day_str,'YYYYMMDD')+1;
v_max_day_str := TO_CHAR(v_max_day,'YYYYMMDD');
v_dates_calender_row.day := TO_CHAR(v_max_day,'YYYYMMDD');
v_dates_calender_row.week := TO_CHAR(v_max_day,'YYYY')||'/'||TO_CHAR(v_max_day,'WW');
v_dates_calender_row.month := TO_CHAR(v_max_day,'YYYY')||'/'||TO_CHAR(v_max_day,'MM');
v_dates_calender_row.quarter := 'Q'||TO_CHAR(FLOOR(TO_NUMBER(TO_CHAR(TRUNC(v_max_day,'Q'),'MM'))/3)+1)||'/'||TO_CHAR(v_max_day,'YYYY');
v_dates_calender_row.year := TO_NUMBER(TO_CHAR(v_max_day,'YYYY'));
EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_TERRITORY = ''AMERICA'' ';
v_dates_calender_row.start_date_week := TO_CHAR(TRUNC(v_max_day, 'IW'),'YYYYMMDD');
v_dates_calender_row.end_date_week := TO_CHAR(TRUNC(v_max_day, 'IW')+6,'YYYYMMDD');
INSERT INTO DATES_CALENDER VALUES v_dates_calender_row;
COMMIT;
END LOOP;
EXCEPTION
WHEN OTHERS
THEN
v_msg_text :='Unexpected Error .'||SQLERRM;
write_to_log(v_module_name,v_msg_text);
RAISE;
END populate_dates_calender;
SELECT day, week, month, quarter, year,
start_date_week,
end_date_week
FROM DATES_CALENDER
WHERE day LIKE '201805%';
DAY WEEK MONTH QUARTER YEAR START_DATE_WEEK END_DATE_WEEK
-------- ---------- ---------- ---------- ----- ------------------ ----------------
20180501 2018/18 2018/05 Q2/2018 2018 20180430 20180506
20180502 2018/18 2018/05 Q2/2018 2018 20180430 20180506
20180503 2018/18 2018/05 Q2/2018 2018 20180430 20180506
20180504 2018/18 2018/05 Q2/2018 2018 20180430 20180506
20180505 2018/18 2018/05 Q2/2018 2018 20180430 20180506
20180506 2018/18 2018/05 Q2/2018 2018 20180430 20180506
20180507 2018/19 2018/05 Q2/2018 2018 20180507 20180513
20180508 2018/19 2018/05 Q2/2018 2018 20180507 20180513
20180509 2018/19 2018/05 Q2/2018 2018 20180507 20180513
20180510 2018/19 2018/05 Q2/2018 2018 20180507 20180513
20180511 2018/19 2018/05 Q2/2018 2018 20180507 20180513
20180512 2018/19 2018/05 Q2/2018 2018 20180507 20180513
20180513 2018/19 2018/05 Q2/2018 2018 20180507 20180513
20180514 2018/20 2018/05 Q2/2018 2018 20180514 20180520
20180515 2018/20 2018/05 Q2/2018 2018 20180514 20180520
20180516 2018/20 2018/05 Q2/2018 2018 20180514 20180520
20180517 2018/20 2018/05 Q2/2018 2018 20180514 20180520
20180518 2018/20 2018/05 Q2/2018 2018 20180514 20180520
20180519 2018/20 2018/05 Q2/2018 2018 20180514 20180520
20180520 2018/20 2018/05 Q2/2018 2018 20180514 20180520
No comments:
Post a Comment