简体   繁体   中英

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM