MySQL + SQL · Lesson 1
Age From Dob
Age from DOB
To calculate exact age from a date of birth, MySQL has the TIMESTAMPDIFF function.
Using TIMESTAMPDIFF
SELECT name, dob,
TIMESTAMPDIFF(YEAR, dob, CURDATE()) AS age
FROM students;name dob age
---- ---------- ---
Aman 2005-03-15 21
How it Works
TIMESTAMPDIFF(YEAR, dob, CURDATE()) counts the full years between the birth date and today, giving the exact age.
Summary
- Use
TIMESTAMPDIFF(YEAR, dob, CURDATE())for exact age in years. - CURDATE() gives today's date.
💻 Live Code Editor
Is page ki language (MYSQL) mein code likhein aur turant chalाएं — yahीं, bina kuch install kiye.Powered by OneCompiler. Agar editor na khule to yahान naye tab mein kholein.
💻 Live Code Editor
Is page ke program yahan ready hain — chalाएं, badlें aur seekhें. Bina kuch install kiye.
Powered by OneCompiler. Editor mein code apne aap aa jata hai — Run dabaakर output dekhें.
Agar load na ho to naye tab mein kholें.