- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate age based on date of birth in MySQL?
Calculate Age based on date of birth with the help of DATE_FORMAT() method in MySQL. Firstly, get the current date time with the help of now() method and you can place your date of birth in DATE_FORMAT().
The syntax is as follows −
SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(now(),'yourDateofbirth')), '%Y')+0 AS anyVariableName;
Apply the above syntax to calculate age from yourDateofbirth. In the above syntax, replace yourDateofbirth with your date of birth. The query is as follows −
SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(now(),'2010-11-25')), '%Y')+0 AS Age;
The following is the output −
+------+ | Age | +------+ | 8 | +------+ 1 row in set (0.00 sec)
Let us now see this in an example. Firstly, create a table −
mysql> create table AgeCalculationFromDatetime -> ( -> YourDateofBirth datetime -> ); Query OK, 0 rows affected (0.52 sec)
Inserting date of birth into table. The query is as follows −
mysql> insert into AgeCalculationFromDatetime values('1995-11-25'); Query OK, 1 row affected (0.13 sec)
Displaying all records with the help of select statement. The query is as follows −
mysql> select *from AgeCalculationFromDatetime;
The following is the output −
+---------------------+ | YourDateofBirth | +---------------------+ | 1995-11-25 00:00:00 | +---------------------+ 1 row in set (0.00 sec)
The query to calculate age is as follows −
mysql> SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(now(),YourDateofBirth)), '%Y')+0 AS Age from AgeCalculationFromDatetime;
Here is the output −
+------+ | Age | +------+ | 23 | +------+ 1 row in set (0.00 sec)`
- Related Articles
- Calculate age from date of birth in MySQL?
- Calculate Age from given Date of Birth in MySQL?
- Finding life path number based on a date of birth in JavaScript
- How to convert birth date records to age with MongoDB
- How to find the age when date of birth is known? Using Java?
- How to calculate time based on seconds in MySQL?
- How to display the day name on the basis of Date of Birth records in MySQL?
- How can we find the employees from MySQL table whose age is greater than say 30 years, providing the only date of birth on the table?
- How to calculate age in years from birthdate in MySQL?
- Return query based on date in MongoDB?
- MySQL DATE_ADD() to increment a date based on the value in another column?
- MySQL query with two boolean conditions to extract date based on hour?
- Select items based on value first, then order on the basis of date for rest of the records in MySQL
- MySQL query to get the dates between range of records displaying student’s Date of Birth?
- MySQL query to return the entire date and time based on a string and format

Advertisements