Weeks are calculated using the IW method. If there are more than or equal to four days between 52nd weeks in a year and December 31 days in that year, it is set to 53rd weeks in that year, otherwise, the remaining days are classified as the 1st week of the next year.
Copy codeThe Code is as follows:
Create or replace function f_week_to_date (a_week varchar2) RETURN CHAR IS
V_first_date char (10 );
V_date_of_week number (1 );
BEGIN
Select to_char (to_date (substr (a_week, 1, 4) | '123', 'yyyymmdd'), 'D ')
Into v_date_of_week
From dual;
V_date_of_week: = v_date_of_week-1;
If v_date_of_week <= 4 then
Select TO_CHAR (TO_DATE (SUBSTR (a_week, 1, 4) | '123', 'yyyymmdd') +
SUBSTR (a_week, 5, 2) * 7-7-v_date_of_week + 1,
'Yyyy-mm-dd ')
Into v_first_date
From dual;
Else
Select TO_CHAR (TO_DATE (SUBSTR (a_week, 1, 4) | '123', 'yyyymmdd') +
SUBSTR (a_week, 5, 2) * 7-v_date_of_week + 1,
'Yyyy-mm-dd ')
Into v_first_date
From dual;
End if;
Return v_first_date;
END;