Add weekdays to date in MySQL

I always seem to need to add weekdays to a date and there doesn’t ever seem to be a solution so here’s mine. This creates a function in your MySQL database that you can use within a statement:

DELIMITER $$
DROP function if exists addWeekDays;
CREATE FUNCTION addWeekDays(startDate DATE, weekdays INT) RETURNS DATE
BEGIN
   WHILE weekdays > 0 DO
      BEGIN
         SET startDate = DATE_ADD(startDate, INTERVAL 1 DAY);
         IF (DAYNAME(startDate) = 'Saturday') THEN SET startDate = DATE_ADD(startDate, INTERVAL 1 DAY); END IF;
         IF (DAYNAME(startDate) = 'Sunday') THEN SET startDate = DATE_ADD(startDate, INTERVAL 1 DAY); END IF;
         SET weekdays=weekdays-1;
      END;
   END WHILE;
   RETURN startDate;
END;$$
DELIMITER ;

Usage:

SELECT
*
FROM
some_table
WHERE
some_date = addWeekDays(DATE(NOW()), 3);