## Forum Stats

• 3,826,571 Users
• 2,260,666 Discussions

Discussions

# Function in param, start_date & number , return sums next working day

Member Posts: 7
edited Sep 17, 2009 2:41AM
Hi Friends,

I need a funtion which can take in parameters as start_date and number , sum them as new date and return new_start_date as workind day.
Suppose '15-SEP-2009' + 6 should return 23-SEP-2009 as new date.
Basically the sum of date and number should return next working day (excluding saturday,sunday).
e.g '15-SEP-2009' + 6 =21-SEP-2009 , but it includes saturday and sunday , so it should return 23-SEP-2009.

Thanks
Niren
Tagged:
«1

• Member Posts: 7
p_days in number,
p_dt in date default trunc(sysdate)
)
return date
as
v_weeks number;
begin

v_weeks := trunc(p_days/5);

if to_number(to_char(p_dt,'D')) + mod(p_days,5) >= 7 then
else
end if;

return p_dt + 7*v_weeks + v_adj+mod(p_days,5);

This is not working correct if the date is SATURDAY, but seems working M-F and Sunday

Output : 19-sep2009 29-Sep-2009
• Member Posts: 7
p_days in number,
p_dt in date default trunc(sysdate)
)
return date
as
v_weeks number;
begin

v_weeks := trunc(p_days/5);

if to_number(to_char(p_dt,'D')) + mod(p_days,5) >= 7 then
else
end if;

return p_dt + 7*v_weeks + v_adj+mod(p_days,5);

This is not working correct if the date is SATURDAY, but seems working M-F and Sunday

Output : 19-sep2009 29-Sep-2009
• Member, Moderator Posts: 42,057 Red Diamond
edited Sep 15, 2009 3:10PM
Hi, Niren,

[This thread|http://forums.oracle.com/forums/thread.jspa?messageID=3351822&#3351822] should give you some ideas. It does it without using PL/SQL, so it's actually a harder problem.

In PL/SQL, you could loop through the days, starting with the start_date parameter and decrementing a counter that is initialized to the other parameter, stopping when the counter reaches 0.
Or you could write a recursive function.
• Member Posts: 5,880 Employee
edited Sep 15, 2009 2:30PM
Like this?
```SQL> ed
Wrote file afiedt.buf

create or replace function next_working_day(start_date in date,  add_days in number) return date is
return_date date;
begin
if (UPPER(to_char(return_date,'FMDAY'))='SATURDAY') then
return_date:=return_date+2;
elsif (UPPER(to_char(return_date,'FMDAY'))='SUNDAY') then
return_date:=return_date+1;
end if;
return return_date;
end;
SQL> /

Function created.

SQL> select next_working_day('10-AUG-2009',10) FROM DUAL;

NEXT_WORK
---------
20-AUG-09

SQL> select next_working_day('11-SEP-2009',1) from dual;

NEXT_WORK
---------
14-SEP-09

SQL> ```
-Arun

Tested

Posted after testing
• Member Posts: 7
Hi Frant,

Please explain me with an example. If you have the exact function/Procedure than it can really help.

Thanks
Niren
• Member, Moderator Posts: 42,057 Red Diamond
edited Sep 15, 2009 3:33PM
Hi, Niren,

The first respondent gave a better function for testing for work days (Monday through Friday). If there's a bug with Saturdays, I'm sure that can fixed. Also,that function depends on your NLS settings. If that's an issue for you, that can be fixed, too. The idea that there are 5 work days in any 7 consecutive days will cut down the execution time considerable.

A function like I suggested would be useful if you had to consider holidays as well as weekends.
Here's an untested example:
```CREATE OR REPLACE FUNCTION  work_days_away
(      in_start_date	    DATE
,      in_day_cnt	    NUMBER
)
RETURN	DATE
DETERMINISTIC
IS
(
IF  in_start_date - TRUNC (in_start_date, 'IW') >= 5
OR  is_holiday (in_start_date)
THEN
RETURN  work_days_away ( in_start_date + 1
, in_day_cnt
);
ELSIF  in_day_cnt >= 1
THEN
RETURN  work_days_away ( in_start_date + 1
, in_day_cnt    - 1
);
ELSE
RETURN  in_start_date;
END IF;
END	work_days_away;```
This use the date format 'IW', which does not depend on NLS settings, rather than 'D', which does.
• Member Posts: 7
Hi Arun,

The code example where 10-AUG-2009 + 10 days should return 22 as per the need but your code is not considering the saturday and sunday falling on 15,16 Aug 2009.

Thanks
Niren
• Member Posts: 5,880 Employee
Hi,

I was under the impression that, after adding the days, if the result falls on saturday / sunday, it should consider that and return the next working day i.e monday.

So, 10-AUG-2009 + 10 = 20-AUG-2009 which falls on Thursday.
11-SEP-2009 + 1 = 12-SEP-2009 which falls on Saturday. So, the next working day is Monday which is 14-Sep-2009.

-Arun
• Member Posts: 7
Hi Arun,

Basically the need is that any number of days added to a date should return date excluding all saturdays and sundays falling in between.
It would be great if you could help me on this .

Thanks
Niren
+919873314561
• Member Posts: 5,880 Employee
edited Sep 16, 2009 5:01AM
```create or replace function add_working_days(
p_days in number,
p_dt in date default trunc(sysdate)
)
return date
as
v_weeks number;
begin

v_weeks := trunc(p_days/5);

if to_number(to_char(p_dt,'D')) + mod(p_days,5) <= 6 then