1

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?

Ricardo Saha
  • 21
  • 1
  • 2
  • 1
    This is [asked](http://stackoverflow.com/questions/42076236/beginner-way-to-count-days-between-dates-excluding-weekends-and-holidays) so [many times](http://stackoverflow.com/questions/41877136/how-to-count-days-except-sundays-between-two-dates-in-postgres) – pozs Apr 11 '17 at 08:47

2 Answers2

2

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)
Vao Tsun
  • 47,234
  • 13
  • 100
  • 132
2

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)
Oto Shavadze
  • 40,603
  • 55
  • 152
  • 236