

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can I get Age using BirthDate column in a MySQL query?
To get Age using BirthDate column in a MySQL query, you can use datediff(). Let us first create a table:
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, DateOfBirth date ); Query OK, 0 rows affected (1.46 sec)
Following is the query to insert some records in the table using insert command:
mysql> insert into DemoTable(DateOfBirth) values('2010-01-21'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(DateOfBirth) values('1993-04-02'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(DateOfBirth) values('1999-12-01'); Query OK, 1 row affected (1.53 sec) mysql> insert into DemoTable(DateOfBirth) values('1998-11-16'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(DateOfBirth) values('2004-03-19'); Query OK, 1 row affected (0.11 sec)
Following is the query to display records from the table using select command:
mysql> select *from DemoTable;
This will produce the following output:
+----+-------------+ | Id | DateOfBirth | +----+-------------+ | 1 | 2010-01-21 | | 2 | 1993-04-02 | | 3 | 1999-12-01 | | 4 | 1998-11-16 | | 5 | 2004-03-19 | +----+-------------+ 5 rows in set (0.00 sec)
Let us now get Age using birthdate column with DATEDIFF() method:
mysql> select cast(DATEDIFF(curdate(),DateOfBirth) / 365.25 AS UNSIGNED) AS AGE from DemoTable;
This will produce the following output:
+------+ | AGE | +------+ | 9 | | 26 | | 19 | | 20 | | 15 | +------+ 5 rows in set (0.00 sec)
Let us now write a query to get the DOB and ID where Age is between 20 and 26:
mysql> SELECT *FROM DemoTable WHERE YEAR(CURDATE())-YEAR(DateOfBirth) BETWEEN 20 AND 26;
This will produce the following output:
+----+-------------+ | Id | DateOfBirth | +----+-------------+ | 2 | 1993-04-02 | | 3 | 1999-12-01 | | 4 | 1998-11-16 | +----+-------------+ 3 rows in set (0.00 sec)
- Related Questions & Answers
- How to calculate age in years from birthdate in MySQL?
- Can I search for particular numbers in a MySQL column with comma separated records using a MySQL query?
- How can I get maximum and minimum values in a single MySQL query?
- Can I get the count of repeated values in a column with MySQL?
- How can I stop running a MySQL query?
- How can I stop a running MySQL query?
- How can I get the output of multiple MySQL tables from a single query?
- How can I get enum possible values in a MySQL database using PHP?
- How to get age from DOB in MySQL?
- Using MySQL, can I sort a column but allow 0 to come last?
- How can I display MySQL query result vertically?
- How can I get the number of times a specific word appears in a column with MySQL?
- How can I remove every column in a table in MySQL?
- How to get an age from a D.O.B field in MySQL?
- MySQL query to get the count of distinct records in a column
Advertisements