
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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)
- Related Articles
- Querying age from DOB in MySQL?
- How to get an age from a D.O.B field in MySQL?
- Get maximum age from records with similar student names in MySQL
- How to calculate age in years from birthdate in MySQL?
- Calculate age from date of birth in MySQL?
- Calculate Age from given Date of Birth in MySQL?
- How to Calculate Age in Excel from Birthday?
- Can I get Age using BirthDate column in a MySQL query?
- How to get day name from timestamp in MySQL?
- How to calculate age from ID number in Excel?
- How to get last 12 digits from a string in MySQL?
- How to get first N characters from a MySQL column?
- How to get field name types from a MySQL database?
- How to get the maximum value from strings with integers in MySQL?
- How to get the second last record from a table in MySQL?

Advertisements