Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
How to get age from DOB in MySQL?
To get age from DOB, you can use the TIMESTAMPDIFF() function. Following is the syntax −
select TIMESTAMPDIFF(YEAR, yourColumnName, CURRENT_DATE) AS anyAliasName from yourTableName;
Let us first create a table −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> DateOfBirth datetime -> ); Query OK, 0 rows affected (0.73 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(DateOfBirth) values('1998-06-04 12:30:00');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable(DateOfBirth) values('2000-01-31 04:10:20');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable(DateOfBirth) values('2010-12-01 03:50:45');
Query OK, 1 row affected (0.24 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+----+---------------------+ | Id | DateOfBirth | +----+---------------------+ | 1 | 1998-06-04 12:30:00 | | 2 | 2000-01-31 04:10:20 | | 3 | 2010-12-01 03:50:45 | +----+---------------------+ 3 rows in set (0.00 sec)
Following is the query to get age from DOB in MySQL −
mysql> select TIMESTAMPDIFF(YEAR, DateOfBirth, CURRENT_DATE) AS YourAge from DemoTable;
Output
+---------+ | YourAge | +---------+ | 21 | | 19 | | 8 | +---------+ 3 rows in set (0.00 sec)
Advertisements
