Skip to content

Instantly share code, notes, and snippets.

@alancoleman
Last active December 22, 2015 05:28
Show Gist options
  • Save alancoleman/6423955 to your computer and use it in GitHub Desktop.
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.
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