Skip to content

Instantly share code, notes, and snippets.

@FlorinAsavoaie
Created December 8, 2015 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FlorinAsavoaie/2531ce57ddccb2d1250f to your computer and use it in GitHub Desktop.
Save FlorinAsavoaie/2531ce57ddccb2d1250f to your computer and use it in GitHub Desktop.
This function returns the number of working days (Monday to Friday) between 2 dates.
-- This function returns the number of working days (Monday to Friday) between 2 dates.
-- Tested on PostGreSQL 9.4.
-- Usage: SELECT working_days('2016-01-01'::date, '2016-01-31'::date);
CREATE OR REPLACE FUNCTION working_days(date, date) RETURNS INT AS
$$
SELECT COUNT(days)::INT
FROM generate_series($1, $2, '1 day') AS days
WHERE EXTRACT(DOW FROM days) NOT IN(0, 6);
$$
LANGUAGE 'sql' IMMUTABLE STRICT;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment