周使用IW方式计算,如果一年当中第52周别之后至当年的12月31日之间,还有大于或等于4天的话,则定为当年的第53周,否则剩余这些天数被归为下一年的第1周
代码如下:
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) || '0101','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,'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,2) * 7 - v_date_of_week + 1,
'yyyy-mm-dd')
into v_first_date
from dual;
end if; return v_first_date; END;
v_first_date char(10);
v_date_of_week number(1); BEGIN
select to_char(to_date(substr(a_week,1,4) || '0101','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,'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,2) * 7 - v_date_of_week + 1,
'yyyy-mm-dd')
into v_first_date
from dual;
end if; return v_first_date; END;