Last active
August 29, 2015 13:57
-
-
Save luizventurote/9902028 to your computer and use it in GitHub Desktop.
SQL function that returns the age.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DROP FUNCTION IF EXISTS fn_idade; | |
DELIMITER $$ | |
CREATE FUNCTION fn_idade(data_nascimento DATE) | |
RETURNS INT | |
LANGUAGE SQL | |
BEGIN | |
RETURN (Year(Curdate()) - Year(data_nascimento)) -(Right(Curdate(),5) < (Right(data_nascimento,5))); | |
END; | |
$$ DELIMITER ; | |
SELECT fn_idade("1994-02-21"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment