-- Oracle 取上周一到周末的sql -- 這樣取的是 在一周內(nèi)第幾天,是以周日為開始的 select to_char(to_date('20130906','yyyymmdd'),'d') from dual; --結(jié)果: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;---上周日 -- 一個(gè)更簡單的寫法 , 返回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; -- 結(jié)果: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;--上周日 -- 獲取上周一的函數(shù) 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; -- 獲取上周日的函數(shù) 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; -- 測(cè)試這個(gè)函數(shù) 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; --查詢結(jié)果:20130826、20130901、20130902、20130908 -- 注: select sysdate from dual; --查詢結(jié)果:2013/9/6 9:45:14
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
