clear markdown feedback
clear markdown feedback
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 3364508 fiddles created (36289 in the last week).

create table tbl (start_date date, end_date date); insert into tbl values ('2017-06-01', '2017-06-01'), ('2017-06-17', '2017-06-17'), ('2017-06-01', '2017-06-12'), ('2017-06-01', '2017-06-13'), ('2017-06-01', '2017-06-16'), ('2017-06-01', '2017-06-17'), ('2017-06-01', '2017-06-18'), ('2017-06-03', '2017-06-12'), ('2017-06-03', '2017-06-13'), ('2017-06-03', '2017-06-16'), ('2017-06-03', '2017-06-17'), ('2017-06-03', '2017-06-18'); SELECT start_date, end_date, ((end_date - start_date) - EXTRACT (isodow FROM end_date) + EXTRACT (isodow FROM start_date)) * 5 / 7 + least(EXTRACT (isodow FROM end_date),5) + 5 - 2 * least(EXTRACT (isodow FROM start_date),5) theDiff FROM tbl
12 rows affected
start_date end_date thediff
2017-06-01 2017-06-01 1
2017-06-17 2017-06-17 0
2017-06-01 2017-06-12 8
2017-06-01 2017-06-13 9
2017-06-01 2017-06-16 12
2017-06-01 2017-06-17 12
2017-06-01 2017-06-18 12
2017-06-03 2017-06-12 6
2017-06-03 2017-06-13 7
2017-06-03 2017-06-16 10
2017-06-03 2017-06-17 10
2017-06-03 2017-06-18 10
 hidden batch(es)