Created
December 8, 2015 22:12
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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