Last active
December 22, 2015 05:28
-
-
Save alancoleman/6423955 to your computer and use it in GitHub Desktop.
Two examples of working with dob in MySQL as age rather than date.
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 dob FROM from table | |
-- all dob between 20 and 25 years of age | |
WHERE FLOOR(DATEDIFF(now(), CAST(dob AS DATE))/365.25 ) BETWEEN 20.0 AND 25.0 | |
-- all dob equal or older than 40 | |
AND Year(Curdate()) - Year(dob) - ( Right(Curdate(),5) < Right(u.dob,5)) <= 40 | |
-- dob within the last six months | |
AND dob >= DATE_SUB(NOW(), INTERVAL 6 MONTH) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment