Here’s a compact, NLS-independent, “no-CONNECT BY” scalar function you can compile once and call from SQL or PL/SQL to return the number of business days (Mon-Fri) between two dates, inclusive.
The implementation is pure PL/SQL math—no row generation—so it is very fast even for large spans.
/* ================================================================
Function : weekdays_between
Purpose : Return the count of Monday-to-Friday days between
p_start_date and p_end_date (inclusive).
Author : <you>
Notes : • If p_start_date > p_end_date ⇒ 0
• NULL in = NULL out
================================================================*/
CREATE OR REPLACE FUNCTION weekdays_between (
p_start_date DATE,
p_end_date DATE )
RETURN PLS_INTEGER
DETERMINISTIC
IS
-- local helpers (all date portions are already TRUNCated)
v_start DATE := TRUNC(p_start_date);
v_end DATE := TRUNC(p_end_date);
v_days PLS_INTEGER; -- total calendar days (incl. both ends)
v_weeks PLS_INTEGER; -- complete 7-day blocks in that span
v_weekdays PLS_INTEGER; -- running business-day total
v_start_dow PLS_INTEGER; -- day-of-week of start (0=Mon .. 6=Sun)
v_rem PLS_INTEGER; -- leftover days after removing weeks
BEGIN
/* --------------------------------------------------------------
0) NULL safety
--------------------------------------------------------------*/
IF p_start_date IS NULL OR p_end_date IS NULL THEN
RETURN NULL;
END IF;
/* --------------------------------------------------------------
1) Ensure chronological order
--------------------------------------------------------------*/
IF v_start > v_end THEN
RETURN 0;
END IF;
/* --------------------------------------------------------------
2) Basic counts
--------------------------------------------------------------*/
v_days := v_end - v_start + 1; -- inclusive span
v_weeks := FLOOR(v_days / 7); -- full 7-day blocks
v_weekdays := v_weeks * 5; -- each full week = 5 biz days
v_rem := MOD(v_days, 7); -- 0 … 6 leftover days
/* --------------------------------------------------------------
3) ISO day-of-week for the start day (0=Mon … 6=Sun)
This calculation is independent of NLS_TERRITORY.
--------------------------------------------------------------*/
v_start_dow := MOD( TRUNC(v_start) - DATE'1900-01-01', 7 );
/* --------------------------------------------------------------
4) Add weekdays contained in the leftover 'partial week'
--------------------------------------------------------------*/
-- Number of Sat/Sun inside the trailing fragment:
-- 1 if the fragment contains 1 weekend day
-- 2 if the fragment contains both
-- 0 otherwise
DECLARE
v_weekend_hits PLS_INTEGER := 0;
BEGIN
-- does the fragment hit a Saturday?
IF v_rem > 0 THEN
-- dow of the first leftover day
DECLARE
v_first_dow PLS_INTEGER := v_start_dow; -- 0=Mon … 6=Sun
BEGIN
-- Saturday = 5, Sunday = 6 in this mapping
IF (6 - v_first_dow) < v_rem AND v_first_dow <= 5 THEN
v_weekend_hits := v_weekend_hits + 1; -- it hits Saturday
END IF;
IF (7 - v_first_dow) < v_rem THEN
v_weekend_hits := v_weekend_hits + 1; -- it hits Sunday
END IF;
END;
END IF;
-- business days in the fragment = total days minus weekend days
v_weekdays := v_weekdays + (v_rem - v_weekend_hits);
END;
RETURN v_weekdays;
END weekdays_between;
/
show errors
Why this version is safe everywhere
• It never calls TO_CHAR(...,'D'), so it is not affected by NLS_TERRITORY settings.
• It processes the range mathematically in O(1) time—no loops longer than 7 iterations, no recursive queries, no CONNECT BY.
• The function is marked DETERMINISTIC, so Oracle can safely memoize it if the optimizer chooses.
Usage examples
-- Simple call
SELECT weekdays_between(DATE '2023-10-09', -- Mon
DATE '2023-10-15') -- Sun
AS biz_days
FROM dual;
/* Result: 5 */
-- Start after end -> 0
SELECT weekdays_between(DATE '2023-10-15', DATE '2023-10-09') AS biz_days
FROM dual;
/* Result: 0 */
-- Same day (falls on weekend) -> 0
SELECT weekdays_between(DATE '2023-10-14', DATE '2023-10-14') AS biz_days
FROM dual;
/* Result: 0 */
-- Same day (weekday) -> 1
SELECT weekdays_between(DATE '2023-10-13', DATE '2023-10-13') AS biz_days
FROM dual;
/* Result: 1 */
Extending for holidays 🌴
If you also need to skip public holidays, create a table (e.g. company_holidays) with one row per holiday and subtract those that fall on weekdays:
SELECT weekdays_between(:p_start, :p_end)
- COUNT(*) -- holidays that fall on Mon-Fri in the span
AS business_days
FROM company_holidays h
WHERE h.holiday_date BETWEEN :p_start AND :p_end
AND TRUNC(h.holiday_date) NOT IN ( -- keep the check safe
SELECT TRUNC(h.holiday_date) FROM dual
WHERE TO_CHAR(h.holiday_date,'DY','NLS_DATE_LANGUAGE=ENGLISH')
IN ('SAT','SUN') );
Now you have a rock-solid, reusable Oracle function for weekday counting with optional holiday subtraction. Happy coding!