Eureka Moment Wiki

One eureka moment at the time

User Tools

Site Tools


database:mysql:weekdays

Count weekdays between two dates in MySql

DELIMITER $$
 
CREATE DEFINER=`su`@`%` FUNCTION `TOTAL_WEEKDAYS`(date1 DATE, date2 DATE) RETURNS INT(11)
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)$$
 
DELIMITER ;

Discussion

Enter your comment:
X​ K F S G
 
database/mysql/weekdays.txt · Last modified: 2019/10/31 09:04 (external edit)