Skip to content

Instantly share code, notes, and snippets.

@luizventurote
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luizventurote/9902028 to your computer and use it in GitHub Desktop.
Save luizventurote/9902028 to your computer and use it in GitHub Desktop.
SQL function that returns the age.
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