mysql-function to count days between 2 dates excluding weekends

前端 未结 4 1144
天涯浪人
天涯浪人 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:10

    UPDATED: If you just need a number of weekdays between two dates you can get it like this

    CREATE FUNCTION TOTAL_WEEKDAYS(date1 DATE, date2 DATE)
    RETURNS INT
    RETURN ABS(DATEDIFF(date2, date1)) + 1
         - ABS(DATEDIFF(ADDDATE(date2, INTERVAL 1 - DAYOFWEEK(date2) DAY),
                        ADDDATE(date1, INTERVAL 1 - DAYOFWEEK(date1) DAY))) / 7 * 2
         - (DAYOFWEEK(IF(date1 < date2, date1, date2)) = 1)
         - (DAYOFWEEK(IF(date1 > date2, date1, date2)) = 7);
    

    Note: The function will still work if you switch start date1 and end date2 dates.

    Sample usage:

    SELECT TOTAL_WEEKDAYS('2013-08-03', '2013-08-21') weekdays1,
           TOTAL_WEEKDAYS('2013-08-21', '2013-08-03') weekdays2;
    

    Output:

    | WEEKDAYS1 | WEEKDAYS2 |
    -------------------------
    |        13 |        13 |
    

    Here is DBFiddle demo

    0 讨论(0)
  • 2020-12-16 07:10

    Had a similar issue, I used PHP to remove the weekends, need to know start day and number of days:

    EG SQL:

      SELECT DAYOFWEEK(`date1`) AS `startday`, TIMESTAMPDIFF(DAY, `date1`, `date2`) AS `interval` FROM `table`
    

    Then run the result through a PHP function:

        function noweekends($startday, $interval) {
            //Remove weekends from an interval
            $wecount = 0; $tmp = $interval;
            while($interval/7 > 1) { $interval-=7; $wecount++; }
            if($interval+$startday > 5) $wecount++;
            $interval = $tmp-($wecount*2);
            return $interval;
        }
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-16 07:28

    I use this. Means there are no functions so can be used in views:

    select 
    datediff(@dateto, @datefrom) + 
    datediff(@datefrom, 
        date_add(@datefrom, INTERVAL 
         floor(datediff(@dateto, @datefrom) / 7) day)) * 2
    - case
        when weekday(@dateto) = 6 then 2
        when weekday(@dateto) = 5 then 1
        when weekday(@dateto) < weekday(@datefrom) then 2
        else 0 
    end;
    
    0 讨论(0)
提交回复
热议问题