Create or replace function udf_weekofyear (v_date in date, formate in varchar2) return char
As
V_ret char (6 );
V_ret1 char (2 );
V_ret3 number;
Begin
V_ret3: = to_char (v_date, 'iw ');
If v_ret3 = 1 and to_char (v_date, 'mm') = '12' then -- determine whether the week of the last month is counted as the first week of the next year
Select to_char (decode (sign (v_date
+ To_number (decode (to_char (trunc (v_date, 'yyyy'), 'D'), '1', '8 ',
TO_CHAR (TRUNC (V_DATE, 'yyyy'), 'D')-2)
-LAST_DAY (V_DATE), 1, LAST_DAY (V_DATE ),
(V_DATE
+ TO_NUMBER (DECODE (TO_CHAR (TRUNC (V_DATE, 'yyyy'), 'D'), '1', '8 ',
TO_CHAR (TRUNC (V_DATE, 'yyyy'), 'D')-2), 'ww ') as week
Into v_ret1 from dual;
Return to_char (v_date, 'yyyy') | v_ret1;
End if;
IF v_ret3 = 53 AND TO_CHAR (v_date, 'mm') = '01' then -- determine whether the week of the first month is counted as the last week of last year, AND then split it into two weeks.
SELECT TO_CHAR (DECODE (SIGN (V_DATE
+ TO_NUMBER (DECODE (TO_CHAR (TRUNC (V_DATE, 'yyyy'), 'D'), '1', '8 ',
TO_CHAR (TRUNC (V_DATE, 'yyyy'), 'D')-2)
-LAST_DAY (V_DATE), 1, LAST_DAY (V_DATE ),
(V_DATE
+ TO_NUMBER (DECODE (TO_CHAR (TRUNC (V_DATE, 'yyyy'), 'D'), '1', '8 ',
TO_CHAR (TRUNC (V_DATE, 'yyyy'), 'D')-2), 'ww ') as week
Into v_ret1 from dual;
Return to_char (v_date, 'yyyy') | v_ret1;
End if;
If to_char (trunc (v_date, 'yyyy'), 'aws') = 53 and to_char (v_date, 'mm ') = '01' then -- determines whether the week of the first month is counted as the last week of last year.
V_ret: = to_char (v_date, formate) + 1; -- push backward for the next week
Return v_ret;
End if;
V_ret: = to_char (v_date, formate); -- processing by calendar week under normal circumstances
Return v_ret;
End;