oracle函数创建,日期加减函数并且要区分工作日与自然日的问题分析
create or replace function func_getlimitdate(in_date date, --输入日期
d_type varchar2, --类型。g:工作日 , z :自然日,2:小时,3:分钟
step number --加上的天数,为负值表示减
--修改为加上的时间,如果是工作日,就是表示加上 step 个工作日;若果是分钟,就是表示加上 step 个分钟
) return date is
/************************************************************
用途:日期加减函数,要区分工作日与自然日(自然日类型不考虑返回的
日期是否处在节假日的问题)
************************************************************/
result date;
v_tempdate date;
v_step number(4, 0);
v_year varchar2(4);
v_month varchar2(4);
v_day varchar2(4);
v_isholiday char;
datetype int default 3;
begin
if d_type = 'g' then
datetype := 0;
end if;
if d_type = 'z' then
datetype := 1;
end if;
if step = 0 then
--加减0天,直接返回
v_tempdate := in_date;
v_year := to_char(v_tempdate, 'yyyy');
v_month := to_number(to_char(v_tempdate, 'mm'));
v_day := to_char(v_tempdate, 'dd');
select isholiday
into v_isholiday
from t_sys_holiday
where year = v_year
and month = v_month
and day = v_day;
if v_isholiday = '0' then
return(in_date);
elsif v_isholiday = '1' then
v_step := 1;
while v_step > 0 loop
if v_step > 0 then
v_tempdate := v_tempdate + 1;
else
v_tempdate := v_tempdate - 1;
end if;
v_year := to_char(v_tempdate, 'yyyy');
v_month := to_number(to_char(v_tempdate, 'mm'));
v_day := to_char(v_tempdate, 'dd');
begin
v_isholiday := null;
select isholiday
into v_isholiday
from t_sys_holiday
where isholiday = '0'
and year = v_year
and month = v_month
and day = v_day;
exception
when no_data_found then
--不是工作日
v_step := v_step + 1;
end;
v_step := v_step - 1;
end loop;
result := v_tempdate;
end if;
elsif datetype = 1 then
--自然日
return(in_date + step);
elsif datetype = 2 then
--小时
return(in_date + step / 24);
elsif datetype = 3 then
--分钟
return(in_date + step / 1440);
else
begin
--工作日
v_tempdate := in_date;
v_step := abs(step);
while v_step > 0 loop
if step > 0 then
v_tempdate := v_tempdate + 1;
else
v_tempdate := v_tempdate - 1;
end if;
v_year := to_char(v_tempdate, 'yyyy');
v_month := to_number(to_char(v_tempdate, 'mm'));
v_day := to_char(v_tempdate, 'dd');
begin
v_isholiday := null;
select isholiday
into v_isholiday
from t_sys_holiday
where isholiday = '0'
and year = v_year
and month = v_month
and day = v_day;
exception
when no_data_found then
--不是工作日
v_step := v_step + 1;
end;
v_step := v_step - 1;
end loop;
result := v_tempdate;
end;
end if;
return(result);
end func_getlimitdate;