skip to Main Content

I want to write a query which will return records where the due date ( will be calculated from start and end date ) is within next 5 business dayes ( exclude weekends)

I tried the following but this is not considering business days:

`SELECT *
FROM table
WHERE (CURRENT_DATE - end_date) <= INTERVAL '5 days' `

2

Answers


  1. You could use DATEDIFF() in mysql

    For example : https://onecompiler.com/mysql/42f8pap49

    For Postgres, just substract you dates like this : https://onecompiler.com/postgresql/42f8q4rmh

    Login or Signup to reply.
  2. For your case (5 work days) simple query

    SELECT *
    FROM test 
    WHERE end_date<=(CURRENT_DATE 
                 +4+case when extract(isodow from CURRENT_DATE)>1 then 2 else 0 end)
    

    For monday(isodow=1) direct add 4 days – friday is due date.
    Else additional 2 days (weekend) add to current date.
    Other, longer, weekends don’t matter.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search