/* 
 * Arrebol Consultancy copyright.
 * 
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited 
 * without written authorization from Arrebol Consultancy.
 */
/**
 * Author:  Carlos Janitzio Zavala Lopez <janitzio.zavala@arrebol.com.mx>
 * Created: 10 nov. 2021
 */
DROP FUNCTION IF EXISTS `totalOfSundaysBetweenDates`;
-- --------------------------------------------------------
--
-- Estructura para la función `totalOfSundaysBetweenDates`
--
DELIMITER //
CREATE FUNCTION `totalOfSundaysBetweenDates` (
    startDate DATE,
    endDate DATE
)
RETURNS INTEGER DETERMINISTIC
BEGIN	
    DECLARE total INTEGER;
    SET total = 0;
    WHILE startDate <= endDate DO
        IF DAYOFWEEK(startDate) = 1 THEN
            SET total = total + 1;
        END IF;
        SET startDate = DATE_ADD(startDate, INTERVAL 1 DAY);
    END WHILE;
RETURN total;
END //
DELIMITER ;