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.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n