Oracle gets the query SQL statement for the date from Monday to the weekend _oracle

Source: Internet
Author: User
Copy Code code as follows:

-Oracle takes the SQL from Monday to the weekend

This takes the first days of the week, starting with Sunday.
Select To_char (to_date (' 20130906 ', ' yyyymmdd '), ' d ') from dual;
--Result: 6 Note: 2013.09.06 is Friday, the sixth day of the week

Select To_char (sysdate+ (2-to_char (sysdate, ' d '))-7, ' YYYYMMDD ') from dual;---Monday
Select To_char (sysdate+ (2-to_char (sysdate, ' d '))-1, ' YYYYMMDD ') from dual;---Sunday

--a simpler formulation that returns the date type
Select Trunc (sysdate, ' IW ')-7 from dual;---Monday
Select Trunc (sysdate, ' IW ')-1 from dual;--on Sunday

This is the Monday.
Select Trunc (sysdate, ' IW ') from dual;

Select Trunc (to_date (' 20130915 ', ' yyyymmdd '), ' IW ') from dual;
--Results: 2013/9/9 Note: 20130915 for Sunday

--Return Char type
Select To_char (trunc (sysdate, ' IW ')-7, ' YYYYMMDD ') from dual;--on Monday
Select To_char (trunc (sysdate, ' IW ')-1, ' YYYYMMDD ') from dual;--on Sunday

--Get the function of the last Monday
Create or Replace function Fun_acc_getlastweekstart (systemdate in date)
Return VARCHAR2 is
Result_str VARCHAR2 (15);
Begin
Select To_char (trunc (systemdate, ' IW ')-7, ' YYYYMMDD ')
Into Result_str
from dual;
return result_str;
End Fun_acc_getlastweekstart;

--Get the function of the last Sunday
Create or Replace function Fun_acc_getlastweekend (systemdate in date) return VARCHAR2 is
Result_str VARCHAR2 (15);
Begin
Select To_char (trunc (systemdate, ' IW ')-1, ' YYYYMMDD ')
Into Result_str
from dual;
return result_str;
End Fun_acc_getlastweekend;

--Test this function
Select Fun_acc_getlastweekstart (sysdate) from dual;
Select Fun_acc_getlastweekend (sysdate) from dual;
Select Fun_acc_getlastweekstart (to_date (' 20130915 ', ' YYYYMMDD ')) from dual;
Select Fun_acc_getlastweekend (to_date (' 20130915 ', ' YYYYMMDD ')) from dual;
--Query results: 20130826, 20130901, 20130902, 20130908
--Note:
Select Sysdate from dual;
--Search results: 2013/9/6 9:45:14
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.