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))) < 0, (((YEAR(date_end) - 1) * 12 + MONTH(date_end)) - ((YEAR(date_start) - 1) * 12 + MONTH(date_start))) + (MID(date_start, 9, 2) < style="color: rgb(102, 204, 102);">(date_end, 9, 2)), (((YEAR(date_end) - 1) * 12 + MONTH(date_end)) - ((YEAR(date_start) - 1) * 12 + MONTH(date_start))))) INTO @num_months; RETURN @num_months; END $$ DELIMITER ;
Hope it helps!
2 comments:
Hi! Great function! Would you consider adding it to MySQL Forge's snippet library? http://forge.mysql.com/snippets/
Snippet added http://forge.mysql.com/snippets/view.php?id=40
Post a Comment