mysql-function to count days between 2 dates excluding weekends

前端 未结 4 1160
天涯浪人
天涯浪人 2020-12-16 07:06

I\'ve searched through many examples , good ones I got :

  1. Count days between two dates, excluding weekends (MySQL only)

  2. How to count date dif

4条回答
  •  别那么骄傲
    2020-12-16 07:18

    This query will work fine, all the queries above are not working well. Try this :

    SELECT ((DATEDIFF(date2, date1)) -
            ((WEEK(date2) - WEEK(date1)) * 2) -
            (case when weekday(date2) = 6 then 1 else 0 end) -
            (case when weekday(date1) = 5 then 1 else 0 end)) as DifD
    

    Test it like this :

    SELECT ((DATEDIFF('2014-10-25', '2014-10-15')) -
                ((WEEK('2014-10-25') - WEEK('2014-10-15')) * 2) -
                (case when weekday('2014-10-25') = 6 then 1 else 0 end) -
                (case when weekday('2014-10-15') = 5 then 1 else 0 end)) as DifD
    

    The result :

    DifD    
    8
    

提交回复
热议问题