-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkyu5_count_weekdays.sql
More file actions
27 lines (26 loc) · 896 Bytes
/
kyu5_count_weekdays.sql
File metadata and controls
27 lines (26 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
-- Count weekdays;
--
-- https://www.codewars.com/kata/58241d05e7a162c5b100010f
--
-- You need to create a function that calculates the number of weekdays (Monday through Friday) between two dates
-- inclusively
--
-- The function should be named weekdays accept two arguments of type DATE and return an INTEGER value:
-- weekdays(DATE, DATE) INTEGER
--
-- The order of arguments shouldn't matter. To illustrate both of the following queries
-- SELECT weekdays('2016-01-01', '2016-01-10');
-- SELECT weekdays('2016-01-10', '2016-01-01');
--
-- should produce the same result:
-- weekdays
-- ---------
-- 6
-- (1 row)
CREATE OR REPLACE FUNCTION weekdays(DATE, DATE) RETURNS INTEGER AS
$$
SELECT COUNT(days)::INTEGER
FROM generate_series(LEAST($1, $2), GREATEST($1, $2), '1 day') AS days
WHERE EXTRACT(DOW FROM days) NOT IN (0, 6);
$$
LANGUAGE SQL;