Oracle query statement from last Monday to weekend

Source: Internet
Author: User

Oracle query statement from last Monday to weekend

[SQL] -- Oracle obtains the SQL statement from the last Monday to the weekend -- in this way, select to_char (to_date ('123 ', 'yyyymmdd'), 'D') from dual; -- result: 6 Note: 2013.09.06 is Friday, which is the sixth day of this week. select to_char (sysdate + (2-to_char (sysdate, 'D')-7, 'yyyymmdd') from dual; --- last Monday select to_char (sysdate + (2-to_char (sysdate, 'D')-1, 'yyyymmdd ') from dual; --- last Sunday -- a simpler statement, returns the date type select trunc (sysdate, 'iw')-7 from dual; --- last Monday select trunc (sysdate, 'aw')-1 from dual; -- last Sunday -- this check shows that this Monday select trunc (sysdate, 'aw') from dual; select trunc (to_date ('123 ', 'yyyymmdd'), 'iw') from dual; -- result: Note: 20130915 is Sunday -- Return char type select to_char (trunc (sysdate, 'iw')-7, 'yyyymmdd') from dual; -- last Monday select to_char (trunc (sysdate, 'iw')-1, 'yyyymmdd') from dual; -- last Sunday -- Obtain the last Monday function 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; -- Obtain the function create or replace function fun_acc_getlastweekend (systemdate in date) of the previous day) 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 transaction; -- test this function select fun_acc_getlastweekstart (sysdate) from dual; select Nation (sysdate) from dual; select fun_acc_getlastweekstart (to_date ('2017010', 'yyyymmdd') from dual; select fun_acc_getlastweekend (to_date ('201312', 'yyyymmdd') from dual; -- Query Result: 20130915, 20130826, 20130901 -- Note: select sysdate from dual; -- Query Result: 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.