Oacle 은 지난 순서의 시작 시간 과 끝 시간의 실현 함 수 를 가 져 옵 니 다.

 
--
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

좋은 웹페이지 즐겨찾기