Oracle 取上週一到週末日期的查詢語句

2021-09-09 03:07:19 字數 1941 閱讀 5855

-- oracle 取上週一到週末的sql

-- 這樣取的是 在一周內第幾天,是以週日為開始的

select to_char(to_date('20130906','yyyymmdd'),'d') from dual;

--結果:6 注釋:2013.09.06是周五,為本週的第六天

select to_char(sysdate+(2-to_char(sysdate,'d'))-7,'yyyymmdd') from dual;---上週一

select to_char(sysdate+(2-to_char(sysdate,'d'))-1,'yyyymmdd') from dual;---上週日

-- 乙個更簡單的寫法 , 返回date型別

select trunc(sysdate,'iw') - 7 from dual;---上週一

select trunc(sysdate,'iw') - 1 from dual;--上週日

-- 這樣查出來是本周一

select trunc(sysdate,'iw') from dual;

select trunc(to_date('20130915','yyyymmdd'),'iw') from dual;

-- 結果:2013/9/9 注釋:20130915 為週日

-- 返回char型別

select to_char(trunc(sysdate,'iw') - 7,'yyyymmdd') from dual;--上週一

select to_char(trunc(sysdate,'iw') - 1,'yyyymmdd') from dual;--上週日

-- 獲取上週一的函式

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;

-- 獲取上週日的函式

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;

-- 測試這個函式

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;

--查詢結果:20130826、20130901、20130902、20130908

-- 注:

select sysdate from dual;

--查詢結果:2013/9/6 9:45:14

Oracle 取上週一到週末日期的查詢語句

oracle 取上週一到週末的sql 這樣取的是 在一周內第幾天,是以週日為開始的 select to char to date 20130906 yyyymmdd d from dual 結果 6 注釋 2013.09.06是周五,為本週的第六天 select to char sysdate 2 ...

Oracle取上週一到週末日期的查詢語句

oracle取上週一到週末日期的查詢語句 sql oracle 取上週一到週末的sql 這樣取的是 在一周內第幾天,是以週日為開始的 select to char to date 20130906 yyyymmdd d from dual 結果 6 注釋 2013.09.06是周五,為本週的第六天 ...

用next day得出本週的周一到週日的日期

用next day得出本週的周一到週日的日期 next day 的語法格式 next day next day date,day of the week 它用來返回從第乙個引數指定的日期開始,第一次到達第二個引數 星期 的日期.引數day of the week可以是從1到7的數字,也可以是sund...