After directing a Devshed poster looking for a way to compute the
number of months between two dates to the manual I decided to turn the solution posted
in user comments by Isaac Shepard into a function, here it
is:
- DELIMITER $$
- DROP FUNCTION IF EXISTS `test`.`months_between` $$
- CREATE FUNCTION `test`.`months_between` (date_start DATE, date_end DATE) RETURNS INT
- BEGIN
- SELECT IF((((YEAR(date_end) - 1) * 12 + MONTH(date_end)) - ((YEAR(date_start) - 1) * 12 + MONTH(date_start))) > 0, (((YEAR(date_end) - 1) * 12 + MONTH(date_end)) - ((YEAR(date_start) - 1) * 12 + MONTH(date_start))) - (MID(date_end, 9, 2) < style="color: rgb(102, 204, 102);">(date_start, 9, 2)), IF((((YEAR(date_end) - 1) * 12 + MONTH(date_end)) - ((YEAR(date_start) - 1) * 12 + MONTH(date_start))) < …