oracle自定義函式剔除假期 使用資料庫

2021-10-18 22:33:51 字數 2313 閱讀 3165

需求:oracle使用資料庫剔除假期

解決方案:

1.建立假期工作日表

create table vacation_work_day

( id varchar2(10

),work_day varchar2(20

),code varchar2(10

))comment on table vacation_work_day

is '2023年工作日與節假日'

;-- add comments to the columns

comment on column vacation_work_day.id

is 'id'

;comment on column vacation_work_day.work_day

is '日期'

;comment on column vacation_work_day.code

is '1:法定假日;0:工作日;

--oracle自增序列建立

create sequence vacation_work_day_idseq

increment by 1

start with 1

minvalue 1

maxvalue 999999999

;2.根據節假日進行sql新增資料,日期code為1為法定節假日,0為工作日

insert into vacation_work_day (id, work_day, code)

values (vacation_work_day_idseq.nextval,

'2021-01-01'

,'0');

insert into vacation_work_day (id, work_day, code)

values (vacation_work_day_idseq.nextval,

'2021-01-02'

,'1');

....

....

3.建立剔除假期自定義函式

create or replace function

fn_duration

(p_arrival_time in varchar2, p_submit_time in varchar2)

/* 內容:獲取時間差

建立人:***x

*/--返回型別

return varchar2

--返回引數

isr_diff_mins varchar2

(100);

v_holiday_count varchar2

(100);

begin

--計算到達與提交日期間包含公休天數

select count(1

) into v_holiday_count

from vacation_work_day

where code !=

0 and work_day between p_arrival_time and p_submit_time;

--計算到達與提交日期間天數

select round

(count(1

)*(1

/24/60

),2)

into r_diff_mins

from (select to_char

(to_date

(p_submit_time,

'yyyy-mm-dd hh24-mi-ss')-

(level /60/

24),'d'

) dow

from dual

connect by level <=

ceil((

to_date

(p_submit_time,

'yyyy-mm-dd hh24-mi-ss')-

to_date

(p_arrival_time,

'yyyy-mm-dd hh24-mi-ss'))

*24*60

));--到達與提交日期間天數 - 到達與提交日期間包含公休、週末天數

return r_diff_mins - v_holiday_count;

end fn_duration;

--測試自定義函式

select fn_duration

('2021-02-01'

,'2021-02-08'

) from dual

--刪除自定義函式

drop function fn_duration;

Oracle自定義函式

語法如下 create or replace function function name argment type,argment type return return type 返回資料的型別 變數的申明,比如 stryuan varchar2 150 begin function body 函...

oracle 自定義函式

下面是乙個前輩寫的判斷是否是手機號的函式 create or replace function ismobile pmsg varchar2 return number isvprefix varchar2 20 vlen number begin vlen lengthb pmsg if vlen...

Oracle自定義函式

二 刪除自定義函式 三 應用經驗 在oracle資料庫中,為了實現特定的功能,可以自定義函式,就像c c 語言,除了系統的庫函式,程式設計師還會編寫很多自定義的函式。create or replace function 函式名 引數1 模式 資料型別,return 資料型別 as 定義區域性變數。變...