频道栏目
首页 > 资讯 > Oracle > 正文

ORACLE函数创建,日期加减函数并且要区分工作日与自然日的问题分析

18-07-09        来源:[db:作者]  
收藏   我要投稿

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;
相关TAG标签
上一篇:mybatis中报错dots问题的分析
下一篇:Sql loader的使用分享
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站