// SQL snippet

Add N business days to a date in SQL

Promises like 'ships in 5 business days' need calendar math that skips weekends.

Main statement
PostgreSQL
Also covers
SQL Server

The task

For each order, list the next 30 calendar days, keep the weekdays, and take the 5th one.

The snippet

Written for PostgreSQL. Versions for SQL Server are under Variations.

PostgreSQL
SELECT o.order_id, o.order_date, c.business_day AS ship_by
FROM orders o
CROSS JOIN LATERAL (
    SELECT d::date AS business_day
    FROM generate_series(o.order_date + 1, o.order_date + 30, interval '1 day') AS g(d)
    WHERE EXTRACT(ISODOW FROM d) < 6
    ORDER BY d
    OFFSET 4 LIMIT 1
) c;

How it works

The LATERAL subquery runs once per order, so generate_series can read that order's date. It produces the next 30 calendar days, the ISODOW filter drops Saturdays and Sundays, and OFFSET 4 LIMIT 1 keeps the fifth surviving day: an order on Wednesday 2026-03-04 ships by Wednesday 2026-03-11. Thirty days is plenty for five business days; widen it for larger N or long holiday runs.

Variations

SQL Server (recursive, skips weekends)

SQL Server
DECLARE @order_date DATE = '2026-03-04';
WITH steps AS (
    SELECT @order_date AS d, 0 AS n
    UNION ALL
    SELECT DATEADD(DAY, 1, d),
           n + CASE WHEN (DATEPART(WEEKDAY, DATEADD(DAY, 1, d)) + @@DATEFIRST - 1) % 7 IN (0, 6)
                    THEN 0 ELSE 1 END
    FROM steps WHERE n < 5
)
SELECT d AS ship_by FROM steps WHERE n = 5;

Questions that come up

How do holidays get in?

Add AND d::date NOT IN (SELECT holiday_date FROM holidays) next to the weekend filter. Holidays vary by country, so a small per-region table beats trying to encode them in the query.

Keep reading

// snippets are better with a schema brain

Run these with autocomplete that knows your tables

SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.