oracle獲取上一旬的開始時(shí)間和結(jié)束時(shí)間的實(shí)現(xiàn)函數(shù)
更新時(shí)間:2013年09月09日 10:14:39 作者:
本文為大家介紹下oracle如何獲取上一旬的開始時(shí)間和結(jié)束時(shí)間,實(shí)現(xiàn)函數(shù)如下,感興趣的朋友可以參考下
復(fù)制代碼 代碼如下:
-- 獲取上旬開始時(shí)間
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 --返回上個(gè)月的下旬
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;
-- 這個(gè)返回的是:上旬的開始日期
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)去 一個(gè) date 類型的值,返回一個(gè)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 --返回上個(gè)月的最后1天
--chr(39) 這個(gè)是加引號(hào)
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;
-- 這個(gè)獲取的是:上旬的結(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 的作用 這個(gè)是一秒
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
您可能感興趣的文章:
- oracle中得到一條SQL語句的執(zhí)行時(shí)間的兩種方式
- oracle 日期時(shí)間函數(shù)使用總結(jié)
- oracle日期時(shí)間型timestamp的深入理解
- Oracle時(shí)間日期操作方法小結(jié)
- oracle 時(shí)間格式的調(diào)整
- Oracle關(guān)于時(shí)間/日期的操作
- 修改計(jì)算機(jī)名或IP后Oracle10g服務(wù)無法啟動(dòng)的解決方法
- 計(jì)算機(jī)名稱修改后Oracle不能正常啟動(dòng)問題分析及解決
- Oracle通過時(shí)間(分鐘)計(jì)算有幾天幾小時(shí)幾分鐘的方法
相關(guān)文章
Oracle undo_management參數(shù)不一致錯(cuò)誤
因RAC的undo_management參數(shù)不一致導(dǎo)致Oracle數(shù)據(jù)庫(kù)mount報(bào)ORA-01105 ORA-01606錯(cuò)誤,本文就這個(gè)問題2013-11-11Oracle生成單據(jù)編號(hào)存儲(chǔ)過程的實(shí)例代碼
Oracle生成單據(jù)編號(hào)存儲(chǔ)過程,在做訂單類似的系統(tǒng)都可能會(huì)存在訂單編號(hào)不重復(fù),或是流水號(hào)按日,按年,按月進(jìn)行重新編號(hào)。下面給大家分享oracle生成單據(jù)編號(hào)存儲(chǔ)過程,需要的的朋友參考下吧2017-04-04Oracle 左連接(+)加號(hào)用法及常用語法之間的關(guān)系
通過分析左連接(+)加號(hào)的寫法和一些常用語法之間的聯(lián)系,了解到Oracle 加號(hào)(+)的用法。本文重點(diǎn)給大家介紹Oracle 左連接(+)加號(hào)用法及常用語法之間的關(guān)系 ,感興趣的朋友跟隨小編一起看看吧2018-10-10Oracle11g RAC開啟關(guān)閉、設(shè)置歸檔小結(jié)
這篇文章主要介紹了Oracle11g RAC開啟關(guān)閉、設(shè)置歸檔,很簡(jiǎn)單,但很實(shí)用,需要的朋友可以參考下2014-09-09Oracle數(shù)據(jù)庫(kù)不同損壞級(jí)別的恢復(fù)教程
這篇文章主要給大家介紹了關(guān)于Oracle數(shù)據(jù)庫(kù)不同損壞級(jí)別的恢復(fù)教程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Oracle數(shù)據(jù)庫(kù)具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Oracle 11gR2中啟動(dòng)Scott用戶的方法(推薦)
這篇文章主要介紹了Oracle 11gR2中啟動(dòng)Scott用戶的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08