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