Exclude Days SQL Function

create or replace FUNCTION EXCLUDE_WEEKEDNDS (p_start_date IN date, p_end_date IN date)
RETURN NUMBER
IS
l_count NUMBER;
BEGIN
l_count := 0;
SELECT COUNT(1) INTO l_count
FROM
(
WITH DATA AS
(SELECT
–to_date(trunc(p_start_date), ‘YYYY-MM-DD’) date1,
–to_date(trunc(p_end_date), ‘YYYY-MM-DD’) date2
p_start_date date1,
p_end_date date2
FROM dual
)
SELECT date1+LEVEL-1 the_date,
TO_CHAR(date1+LEVEL-1, ‘DY’,’NLS_DATE_LANGUAGE=AMERICAN’) day
FROM DATA
WHERE TO_CHAR(date1+LEVEL-1, ‘DY’,’NLS_DATE_LANGUAGE=AMERICAN’)
NOT IN (‘SAT’, ‘SUN’)
CONNECT BY LEVEL <= date2-date1+1
);

RETURN l_count;
END ;

select EXCLUDE_WEEKEDNDS (to_date(‘2021/06/01′,’YYYY/MM/DD’) ,to_date(‘2021/06/30′,’YYYY/MM/DD’) ) workdays from dual;

Leave a Reply

Your email address will not be published. Required fields are marked *