junior
junior

Reputation: 496

Calculate the difference between two dates in business days

I'm trying to calculate the difference in business days between two dates, at my searches I found the use of functions and gereneate_series() but I would like to search something more practical.

Sample Table:

|start_date |end_date |
|2022-06-01 |2022-06-01| 
|2022-05-29 |2022-06-02| 

Upvotes: 1

Views: 1845

Answers (1)

Belayer
Belayer

Reputation: 14934

What would you consider more practical? It seems generate_series and extract are specifically made for what you are asking. (see demo)

select start_date, end_date, count(*)   "Business days"
  from sometable 
 cross join generate_series(start_date,end_date,interval '1 day') gs(dt) 
 where extract(isodow from dt) < 6  
 group by start_date, end_date;

Upvotes: 2

Related Questions