If do:
SELECT '2017-03-31'::DATE - '2017-03-01'::DATE AS daysCount
it returns days count between those dates. I need same but excluding all Saturdays and Sundays. How?
If do:
SELECT '2017-03-31'::DATE - '2017-03-01'::DATE AS daysCount
it returns days count between those dates. I need same but excluding all Saturdays and Sundays. How?
you can use dow
to exclude them, eg:
t=# with i as (
select '2017-03-31'::DATE d1, '2017-03-01'::DATE d2
)
select count(1)
from i
join generate_series(d2,d1,'1 day'::interval) g on true
where extract(dow from g) not in (6,0);
count
-------
23
(1 row)
t=# with i as (
select '2017-04-30'::DATE d1, '2017-04-01'::DATE d2
)
select count(1)
from i
join generate_series(d2,d1,'1 day'::interval) g on true
where extract(dow from g) not in (6,0);
count
-------
20
(1 row)
If I correctly understand, you need this:
select count(*) from (
select EXTRACT(DOW FROM s.d::date) as dd from generate_series('2017-03-01'::DATE, '2017-03-31'::DATE , '1 day') AS s(d)
) t
where dd not in(0,6)