oracle獲取上一旬的開始時間和結(jié)束時間的實現(xiàn)函數(shù)
更新時間:2013年09月09日 10:14:39 作者:
本文為大家介紹下oracle如何獲取上一旬的開始時間和結(jié)束時間,實現(xiàn)函數(shù)如下,感興趣的朋友可以參考下
復(fù)制代碼 代碼如下:
-- 獲取上旬開始時間
create or replace function fd_lastxunstart(rq in date) return string is
refstr varchar2(50);
v_rq date;
begin
--獲取上一旬的日期
v_rq := trunc(rq);
select case decode(trunc((to_char(v_rq, 'dd') - 1) / 10),
0,
'上旬',
1,
'中旬',
'下旬')
when '上旬' then --返回上個月的下旬
to_char(add_months(v_rq, -1), 'yyyyMM') || '21'
when '中旬' then
to_char(v_rq, 'yyyymm') || '01' else
to_char(v_rq, 'yyyymm') || '11'
end
into refstr
from dual;
return refstr;
end fd_lastxunstart;
-- 這個返回的是:上旬的開始日期
select sysdate from dual;
select fd_lastxunstart(sysdate) from dual;
select fd_lastxunstart(to_date('20130305','yyyymmdd')) from dual;
select fd_lastxunstart(to_date('20130311','yyyymmdd')) from dual;
select fd_lastxunstart(to_date('20130325','yyyymmdd')) from dual;
-- 執(zhí)行結(jié)果為: 2013/9/5 12:08:39、20130821、20130221、20130301、20130311
---- 獲取上一旬的結(jié)束日期
-- 傳遞進(jìn)去 一個 date 類型的值,返回一個varchar類型的上旬結(jié)束日期
create or replace function fd_lastxunend(rq in date) return string is
refstr varchar2(50);
v_rq date;
begin
--獲取上一旬的日期
v_rq := trunc(rq);
select case decode(trunc((to_char(v_rq, 'dd') - 1) / 10),
0,
'上旬',
1,
'中旬',
'下旬')
when '上旬' then --返回上個月的最后1天
--chr(39) 這個是加引號
to_char(last_day(add_months(v_rq, -1)) + 1 - 1 / 24 / 60 / 60,
'yyyymmdd')
when '中旬' then
to_char(v_rq, 'yyyymm') || '10' else
to_char(v_rq, 'yyyymm') || '20'
end
into refstr
from dual;
return refstr;
end fd_lastxunend;
-- 這個獲取的是:上旬的結(jié)束日期
select fd_lastxunend(sysdate) from dual;
select fd_lastxunend(to_date('20130305','yyyymmdd')) from dual;
select fd_lastxunend(to_date('20130311','yyyymmdd')) from dual;
select fd_lastxunend(to_date('20130315','yyyymmdd')) from dual;
select fd_lastxunend(to_date('20130221','yyyymmdd')) from dual;
--執(zhí)行結(jié)果:20130831、20130228、20130310、20130310、20130220
-- 觀察 1 / 24 / 60 / 60 的作用 這個是一秒
select last_day(add_months(trunc(sysdate), -1)) + 1 - 1 / 24 / 60 / 60
from dual;
select last_day(add_months(trunc(sysdate), -1)) from dual;
select last_day(add_months(trunc(sysdate), -1)) + 1 from dual;
-- 執(zhí)行結(jié)果:2013/8/31 23:59:59、2013/8/31、2013/9/1
相關(guān)文章
關(guān)于ORA-04091異常的出現(xiàn)原因分析及解決方案
這篇文章主要介紹了關(guān)于ORA-04091異常的出現(xiàn)原因分析及解決方案,本文給大家分享異常出現(xiàn)的場景及解決代碼,感興趣的朋友跟隨小編一起看看吧2023-05-05
Oracle 12CR2查詢轉(zhuǎn)換教程之cursor-duration臨時表詳解
這篇文章主要給大家介紹了關(guān)于Oracle 12CR2查詢轉(zhuǎn)換教程之cursor-duration臨時表的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
基于Oracle的高性能動態(tài)SQL程序開發(fā)
對動態(tài)SQL的程序開發(fā)進(jìn)行了總結(jié),并結(jié)合筆者實際開發(fā)經(jīng)驗給出若干開發(fā)技巧2007-03-03
oracle使用sql腳本生成csv文件案例學(xué)習(xí)
在oracle中用sql腳本生成csv文件,很多的朋友都想實現(xiàn)這樣的功能,所以本文的出現(xiàn)是很有必要的,感興趣的你可千萬不要錯過,希望本文可以幫助到你2013-02-02

