create or replace function networkday(p_date1 date,p_date2 date)
return number
is
p_num number;
week_num number;
mod_num number;
extra_weekend number;
rs number;
begin
mod_num := mod(p_date2 - p_date1 + 1,7);
week_num := (p_date2 - p_date1 + 1 - mod_num)/7;
p_num := p_date1 - trunc(p_date1,'IW') - 7;
extra_weekend := case
when week_num > 0 and p_num = -2 and mod_num = 1 then 1
when week_num > 0 and p_num = -2 and mod_num >= 2 then 2
when week_num > 0 and p_num = -1 and mod_num >= 1 then 1
when week_num > 0 then 0
when p_num + mod_num - 1 >= -1 then 2
when p_num + mod_num - 1 >= -2 then 1
else 0 end;
rs := p_date2 - p_date1 + 1 - (week_num * 2 + extra_weekend);
return rs;
end;