Oracle's week date function that meets Chinese habits

Source: Internet
Author: User
Oracle's date function compute week is the first day of Sunday and the last day of Sunday. The Chinese are used to counting the first day on Monday and the last day on Sunday. In addition, Oracle Functions

Oracle's date function compute week is the first day of Sunday and the last day of Sunday. The Chinese are used to counting the first day on Monday and the last day on Sunday. In addition, Oracle Functions

Recently, we helped HR capture the overtime records. HR wants to calculate the number of overtime hours by week. However, Oracle's date function compute week is the first day of Sunday and the last day of Sunday. The Chinese are used to counting the first day on Monday and the last day on Sunday. In addition, Oracle functions return the week number, which is a number, and HR also calculates the date segment based on the data. I wrote a small function that meets our requirements. The returned values are also much more scientific!

FUNCTION get_wk (p_date in date)

RETURN VARCHAR2

IS

V_wk VARCHAR2 (30 );

V_n NUMBER;

BEGIN

SELECT TO_NUMBER (TO_CHAR (p_date, 'D '))

INTO v_n

From dual;



IF v_n = 1

THEN

V_wk: =

TO_CHAR (p_date-6, 'yyyy/MM/dd ')

| '-'

| TO_CHAR (p_date, 'yyyy/MM/dd ');

ELSE

V_wk: =

TO_CHAR (p_date + (7-(5 + v_n), 'yyyy/MM/dd ')

| '-'

| TO_CHAR (p_date + (7-(5 + v_n) + 6, 'yyyy/MM/dd ');

End if;



RETURN v_wk;

END;

Related reading:

Replace () for Oracle Functions ()

Oracle Functions

Differences in case and decode usage and Performance Comparison of Oracle Functions

Simple Oracle functions and stored procedures

The Oracle function obtains the time period in seconds or minutes.

,

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.