DELIMITER $$ CREATE OR REPLACE DEFINER=`root`@`localhost` FUNCTION `vn`.`getDueDate`(vDated DATE, vDayToPay INT) RETURNS date NOT DETERMINISTIC NO SQL BEGIN DECLARE vDued DATE; IF vDayToPay = 0 THEN RETURN vDated; END IF; SET vDued = TIMESTAMPADD(DAY,vDayToPay MOD 30, LAST_DAY(TIMESTAMPADD(MONTH, vDayToPay DIV 30, vDated))); RETURN vDued; END$$ DELIMITER ;