Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Age using BirthDate column by MySQL query?

I have a BirthDate column in MySQL database table to store user's date of birth. Now I have form in html/php with two fields(1. Age From 2. Age To).

If user want to get all users where their ages are between 10 years and 20 years, Is it possible to do this with MySQL query using BirthDate column.

Thanks

like image 924
Awan Avatar asked Apr 08 '11 07:04

Awan


People also ask

How do you find age in SQL query?

We can find the age of a person from their date of birth by using this formula: SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),'DATE_OF_BIRTH')), '%Y') + 0 AS age; In the above formula, we are first finding the difference between the current date (NOW()) and the date of birth (in YYYY-MM-DD) using the DATEDIFF() function.

What is the data type for dob in MySQL?

MySQL retrieves and displays DATE values in ' YYYY-MM-DD ' format. The supported range is '1000-01-01' to '9999-12-31' . The DATETIME type is used for values that contain both date and time parts.

How can get date in dd mm yyyy format in MySQL?

The following is the output. The following is the query to format the date to YYYY-MM-DD. mysql> select str_to_date(LoginDate,'%d. %m.

How do I find the age between two dates in SQL?

Using SQL's DateDiff() for Age The short solution is to use the built-in function DATEDIFF( ) where you are able to find the year difference between two dates.


2 Answers

Your query would look similar to this:

SELECT * FROM your_table WHERE YEAR(CURDATE())-YEAR(BirthDate) BETWEEN 10 AND 20;
like image 197
soulkphp Avatar answered Sep 19 '22 02:09

soulkphp


You can get like this

SELECT column1, column2, 
   DATE_FORMAT(FROM_DAYS(TO_DAYS(now()) - TO_DAYS(dob)), '%Y') + 0 as age     
WHERE age >10 AND age <20 ; 
like image 21
Shakti Singh Avatar answered Sep 21 '22 02:09

Shakti Singh