Sam Krausz
Sam Krausz

Reputation: 1

How Do I Calculate the Number of Weekday Between Two Dates in Postgresql

I only need the difference of Weekdays, NOT weekends

Here is my SQL Statement:

SELECT
created_time_receiving [1] :: DATE - created_time_order_items :: DATE AS days
FROM order_items

Upvotes: 0

Views: 700

Answers (1)

romainsalles
romainsalles

Reputation: 2143

You can use the isodow function:

The day of the week as Monday(1) to Sunday(7)

Combinated to the generate_series(start, stop, step) function:

Generate a series of values, from start to stop with a step size of step

To make this query:

SELECT
  count(*) AS days
FROM
  generate_series('2021-02-02', '2021-06-02', interval '1d') d
WHERE
  extract('isodow' FROM d) < 6;

Upvotes: 2

Related Questions