/*===========================================================================
Description: To derive the date of the given year week and day of week.
Assumption: First day of the week = monday.
Eg 199627, 'MON' --> 01-JUL-1996 (Monday of Week 27 in 1996)
Input : p_year_week - Input year week in YYYYWW format
p_day_of_week - 'MONDAY' - 'SUNDAY'.
Returns : date of the given year week and day of week in DATE format.
===========================================================================
*/
FUNCTION yw_to_date(p_year_week NUMBER,
p_day_of_week VARCHAR2) RETURN DATE IS
w_year NUMBER(4);
w_week NUMBER(2);
w_date DATE;
BEGIN
IF p_year_week IS NULL THEN
RETURN null;
END IF;
w_year := substr(p_year_week,1,4);
w_week := to_number(substr(p_year_week,5,2));
-- Get first MONDAY of week 1
SELECT next_day(to_date('28-DEC-'||to_char(w_year-1),'DD-MON-YYYY'),'MONDAY')
INTO w_date
FROM dual;
w_date := w_date + (w_week-1)*7;
IF upper(p_day_of_week) <> 'MONDAY' THEN
w_date := next_day(w_date,p_day_of_week);
END IF;
RETURN w_date;
END yw_to_date;