Calculate number of day
mysql>
mysql> delimiter $$
mysql>
mysql> CREATE FUNCTION NUMBER_OF_DAYS
-> (START_DATE DATE,
-> END_DATE DATE)
-> RETURNS INTEGER
-> BEGIN
-> DECLARE DAYS INTEGER;
-> DECLARE NEXT_DATE, PREVIOUS_DATE DATE;
-> SET DAYS = 0;
-> SET NEXT_DATE = START_DATE + INTERVAL 1 DAY;
-> WHILE NEXT_DATE <= END_DATE DO
-> SET DAYS = DAYS + 1;
-> SET PREVIOUS_DATE = NEXT_DATE;
-> SET NEXT_DATE = NEXT_DATE + INTERVAL 1 DAY;
-> END WHILE;
-> RETURN DAYS;
-> END$$
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql> delimiter ;
mysql>
Related examples in the same category