Oracle calendar week cross-year week is divided into two parts

Source: Internet
Author: User

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;

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.