国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > Oracle > 正文

oracle獲取上一旬的開始時間和結束時間的實現函數

2024-08-29 13:57:02
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:


-- 獲取上旬開始時間
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;

-- 執行結果為: 2013/9/5 12:08:39、20130821、20130221、20130301、20130311

---- 獲取上一旬的結束日期
-- 傳遞進去 一個 date 類型的值,返回一個varchar類型的上旬結束日期
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;

-- 這個獲取的是:上旬的結束日期
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;

--執行結果: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;
-- 執行結果:2013/8/31 23:59:59、2013/8/31、2013/9/1

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 雷波县| 西畴县| 西峡县| 龙岩市| 海兴县| 纳雍县| 望奎县| 名山县| 灵宝市| 潞西市| 霍州市| 郑州市| 大名县| 手游| 博罗县| 宜章县| 大英县| 广东省| 牙克石市| 英德市| 濉溪县| 临澧县| 德清县| 镇康县| 建昌县| 扶沟县| 牟定县| 图木舒克市| 墨竹工卡县| 镇原县| 行唐县| 牙克石市| 武山县| 原平市| 临汾市| 阆中市| 城固县| 德保县| 比如县| 合作市| 福海县|