How to count date difference excluding weekend and holidays in MySQL

后端 未结 5 1929
青春惊慌失措
青春惊慌失措 2020-12-01 12:35

I need to count days (business days) between two dates excluding weekend (most important) and holidays

SELECT DATEDIFF(end_date, start_date) from accounts


        
相关标签:
5条回答
  • 2020-12-01 13:03

    Create a table that contains all the weekends and holidays for the next 100whatever years.

    You need to be able to specify when a day is a 'holiday' given that no one knows what the holidays will be for 2052 yet, you will not be able to make an accurate function at this time anyway. just update your non-work day table each year when the holidays become known (but you will always know the weekends).

    Then your query becomes:

    SELECT DATEFIFF(end_date, start_date) - COALESCE((SELECT COUNT(1) FROM nonWorkDays WHERE nonWorkDays.date BETWEEN start_date AND end_date), 0)
    FROM accounts
    

    If you really need to write a DATEDIFFWITHOUTWEEKENDSORHOLIDAYS function then just use the above and create a function (there's plenty of resources on how to make functions in each RDBMS).. just be sure to give it a better name. ^_^

    One thing you will need to fix is I think there's a +1 missing somewhere in the above, for example DATEDIFF(today, today) if today is a weekend will return -1 instead of returning 0.

    0 讨论(0)
  • 2020-12-01 13:03

    Make a function that will make a while cycle between the dates incrementing the number of days when it's not a saturday or sunday.

    0 讨论(0)
  • 2020-12-01 13:05

    Something like this may work. Add all holiday dates and weekend dates to a table.

    SELECT 
      DATEDIFF(end_date, start_date) 
    FROM table
    WHERE date NOT IN (SELECT date FROM holidaydatestable )
    
    0 讨论(0)
  • 2020-12-01 13:05

    Try This Code this will Calculate no of days Excluding Weekends

     SELECT
           (DATEDIFF(dd, @StartDate, @EndDate)+1)
          -(DATEDIFF(wk, @StartDate, @EndDate) * 2)
    from test_tbl where date NOT IN (SELECT date FROM holidaydatestable )
    
    0 讨论(0)
  • 2020-12-01 13:17

    You might want to try this:

    1. Count the number of working days (took it from here)

      SELECT 5 * (DATEDIFF('2012-12-31', '2012-01-01') DIV 7) + MID('0123444401233334012222340111123400012345001234550', 7 * WEEKDAY('2012-01-01') + WEEKDAY('2012-12-31') + 1, 1)

      This gives you 261 working days for 2012.

    2. Now you need to know your holidays that are not on a weekend

      SELECT COUNT(*) FROM holidays WHERE DAYOFWEEK(holiday) < 6

      The result of this depends on your holiday table.

    3. We need to get that in one query:

      SELECT 5 * (DATEDIFF('2012-12-31', '2012-01-01') DIV 7) + MID('0123444401233334012222340111123400012345001234550', 7 * WEEKDAY('2012-01-01') + WEEKDAY('2012-12-31') + 1, 1) - (SELECT COUNT(*) FROM holidays WHERE DAYOFWEEK(holiday) < 6)

      This should be it.

    Edit: Please be aware that this only works properly if your end date is higher than your start date.

    0 讨论(0)
提交回复
热议问题