Question: I need to write an sql query that returns the number of Working days (Monday - Friday) between two given dates.

I was wondering what would be the most efficient way to do this?

```
SELECT           --Start with total number of days including weekends             
(DATEDIFF(dd,@StartDate,@EndDate)+1) --Subtact 2 days for each full weekend 
(DATEDIFF(wk,@StartDate,@EndDate)*2) --If StartDate is a Sunday, Subtract 1          
ELSE 0               END)            --If EndDate is a Saturday, Subtract 1 
FROM dual

```

Then it would also be helpful to be able to remove holidays from this count such as christmas day and boxing day.

Any Ideas?

Answer: 
Here is an example

```
with given_days(d) as(
  select <<start_date>> + level - 1
    from dual
  connect by level < = (<<end_date>> - <<start_date>>) + 1
)
select count(*)
  from given_days
where to_char(d, 'DY', 'NLS_DATE_LANGUAGE=english') not in ('SUN', 'SAT')

```

Demonstration

```
HR\XE> with given_days as(
  2    select (to_date('&&1', 'dd.mm.yyyy') + level - 1) as g_day
  3      from dual
  4    connect by level < = (to_date('&2', 'dd.mm.yyyy') - to_date('&&1', 'dd.mm.yyyy')) + 1
  5  )
  6  select count(g_day) as cnt
  7    from given_days
  8  where to_char(g_day, 'DY', 'NLS_DATE_LANGUAGE=english') not in ('SUN', 'SAT');
Enter value for 1: 10.10.2012
old   2:   select to_date('&&1', 'dd.mm.yyyy') + level - 1
new   2:   select to_date('10.10.2012', 'dd.mm.yyyy') + level - 1
Enter value for 2: 17.10.2012
old   4:   connect by level < = (to_date('&2', 'dd.mm.yyyy') - to_date('&&1', 'dd.mm.yyyy')) + 1
new   4:   connect by level < = (to_date('17.10.2012', 'dd.mm.yyyy') - to_date('10.10.2012', 'dd.mm.yyyy')) + 1

  cnt                                                                     
----------                           
   6                                                                      

```
Here you go...

1. First check how many days you got in the holiday table, excluding weekend days.
2. Get business days (MON to FRI) between the 2 dates and after that subtract the holiday days.

```
create or replace
FUNCTION calculate_business_days (p_start_date IN DATE, p_end_date IN DATE)
        RETURN NUMBER IS
        v_holidays     NUMBER;
        v_start_date   DATE   := TRUNC (p_start_date);
        v_end_date     DATE   := TRUNC (p_end_date);
        BEGIN
        IF v_end_date >= v_start_date
        THEN
                SELECT COUNT (*)
                INTO v_holidays
                FROM holidays
                WHERE day BETWEEN v_start_date AND v_end_date
                AND day NOT IN (
                        SELECT hol.day 
                        FROM holidays hol 
                        WHERE MOD(TO_CHAR(hol.day, 'J'), 7) + 1 IN (6, 7)
                );

        RETURN   GREATEST (NEXT_DAY (v_start_date, 'MON') - v_start_date - 2, 0)
             +   (  (  NEXT_DAY (v_end_date, 'MON')
                     - NEXT_DAY (v_start_date, 'MON')
                    )
                  / 7
                 )
               * 5
             - GREATEST (NEXT_DAY (v_end_date, 'MON') - v_end_date - 3, 0)
             - v_holidays;
        ELSE
                RETURN NULL;
        END IF;
END calculate_business_days;

```

After that you can test it out, like: 

```
    select 
            calculate_business_days('21-AUG-2013','28-AUG-2013') as business_days 
    from dual;

```