Plainfold calendar reference

Business days between two dates in SQL

Counting business days between two dates in SQL needs two things: a weekday test and a holiday table. The queries below count the business days d with start ≤ d < end (the start counts, the end does not, like numpy's busday_count; Excel's NETWORKDAYS counts both ends). Each was run on PostgreSQL 16 and DuckDB and compared with numpy: the on-the-fly count on random 2026 date ranges (2,000 on DuckDB, 300 on PostgreSQL), the other queries on the examples shown.

1. A holiday table

-- A holiday table (one row per weekday closure). Load yours, or the free CSVs linked below.
CREATE TABLE holidays (holiday_date date PRIMARY KEY, name text);
INSERT INTO holidays VALUES
  ('2026-01-01', 'New Year''s Day'), ('2026-01-19', 'Martin Luther King Jr. Day'),
  ('2026-02-16', 'Washington''s Birthday'), ('2026-05-25', 'Memorial Day'),
  ('2026-06-19', 'Juneteenth'), ('2026-09-07', 'Labor Day'), ('2026-10-12', 'Columbus Day'), ('2026-11-11', 'Veterans Day'),
  ('2026-11-26', 'Thanksgiving Day'), ('2026-12-25', 'Christmas Day');

This example uses the 2026 Federal Reserve (US bank) holidays: a Saturday holiday such as July 4, 2026 is observed by federal offices on Friday July 3, but the Federal Reserve stays open that Friday, so check which calendar you need. For the NYSE, add Good Friday and July 3, 2026 (the exchange closes the Friday before a Saturday holiday) and drop Columbus Day and Veterans Day.

2. Count on the fly

-- PostgreSQL: business days d with start_date <= d < end_date
SELECT count(*) AS business_days
FROM generate_series(DATE '2026-11-20', DATE '2026-12-31' - 1, INTERVAL '1 day') AS g(d)
WHERE extract(isodow FROM g.d) < 6
  AND NOT EXISTS (SELECT 1 FROM holidays h WHERE h.holiday_date = g.d::date);  -- g.d is a timestamp: cast
-- DuckDB: same count (range() excludes the end date)
SELECT count(*) AS business_days
FROM range(DATE '2026-11-20', DATE '2026-12-31', INTERVAL 1 DAY) AS g(d)
WHERE isodow(g.d) < 6
  AND NOT EXISTS (SELECT 1 FROM holidays h WHERE h.holiday_date = CAST(g.d AS DATE));

Both return 27: November 20 to December 30, 2026, minus weekends, Thanksgiving and Christmas.

3. Precompute a calendar table

-- PostgreSQL and DuckDB: precompute a calendar table once (cover every date you will query)
CREATE TABLE business_days AS
SELECT d::date AS calendar_date,
       CASE WHEN extract(isodow FROM d) < 6
             AND NOT EXISTS (SELECT 1 FROM holidays h WHERE h.holiday_date = d::date)
            THEN 1 ELSE 0 END AS is_business_day
FROM generate_series(DATE '2026-01-01', DATE '2027-12-31', INTERVAL '1 day') AS g(d);

SELECT sum(is_business_day) AS business_days
FROM business_days
WHERE calendar_date >= DATE '2026-11-20' AND calendar_date < DATE '2026-12-31';

To count for every row of a large table, store a running business-day number per date and subtract (join your table to it once on the start date and once on the end date):

-- Running number: business days before each date, so any count is one subtraction
CREATE TABLE business_day_numbers AS
SELECT calendar_date, is_business_day,
       sum(is_business_day) OVER (ORDER BY calendar_date) - is_business_day AS bd_before
FROM business_days;

-- business days d with start <= d < end, for every row of a table, without scanning ranges
SELECT e.bd_before - s.bd_before AS business_days
FROM business_day_numbers s, business_day_numbers e
WHERE s.calendar_date = DATE '2026-11-20' AND e.calendar_date = DATE '2026-12-31';

Also 27. A start or end date on a weekend or holiday needs no special case; the count is still the business days in between. If the end is before the start, sections 2 and 3 return 0 and the running-number query returns a negative count.

4. Add business days to a date

-- Add 10 business days to a date (PostgreSQL and DuckDB; the 10th business day after start)
SELECT calendar_date AS due_date
FROM business_days
WHERE calendar_date > DATE '2026-11-20' AND is_business_day = 1
ORDER BY calendar_date
OFFSET 9 LIMIT 1;

Returns 2026-12-07. It returns no row if the calendar table ends too early, so build it a year or two past your last date. In Excel, NETWORKDAYS(start, end - 1, holidays) gives the same counts as these queries when end is after start.

Other engines need their own date series (a recursive CTE) and weekday function (MySQL DAYOFWEEK, SQL Server DATEPART(weekday), which depends on DATEFIRST, BigQuery and Snowflake EXTRACT(DAYOFWEEK)); they are not tested here.

Ready-made holiday tables: free CSVs for US federal, NYSE and Fed holidays, currency settlement holidays and stock exchanges, and a dbt package with business_days_between macros. Python: pip install usbizdays.

Free: pip install usbizdays (is_business_day, add_business_days for federal, NYSE and Fed calendars) and sample CSV/.ics files at github.com/plainfoldhq/us-federal-calendar.

Full set ($15): federal holidays 1971-2050, NYSE trading days, closures and early closes and Fed holidays 2000-2050 as CSV, JSON, SQLite and .ics, plus a tested PostgreSQL loader. Get the full set

All three data packs (business days, date dimension, time zones): $35. Bundle

Every data pack, yearly: $49/year. All-Access