Created
August 13, 2011 05:26
-
-
Save williamn/1143505 to your computer and use it in GitHub Desktop.
Find age SQL function
This file contains hidden or 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
SELECT DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(dob, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(dob, '00-%m-%d')) AS age |
This file contains hidden or 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
SELECT name, dob, YEAR(CURRENT_DATE) | |
- YEAR(dob) | |
- CASE WHEN MONTH(CURRENT_DATE) | |
> MONTH(dob) | |
THEN 0 | |
WHEN MONTH(CURRENT_DATE) | |
< MONTH(dob) | |
THEN 1 | |
WHEN DAYOFMONTH(CURRENT_DATE) | |
< DAYOFMONTH(dob) | |
THEN 1 | |
ELSE 0 END AS age | |
FROM people |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment