Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date Difference in MySQL to calculate age

I have a problem regarding the datediff MYSQL function, I can use it and it is simple. But I don't understand how to use it to collect differences within the table field. E.g.

I have a column dob and I want to write a query that will do something like

select dateDiff(current_timeStamp,dob) 
from sometable 'here dob is the table column

I mean I want the difference from the current date time to the table field dob, each query result is the difference, the age of the user.

like image 549
chsab420 Avatar asked Sep 05 '09 14:09

chsab420


People also ask

How can I get age from 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.

How does MySQL calculate date difference in time?

MySQL TIMEDIFF() Function The TIMEDIFF() function returns the difference between two time/datetime expressions. Note: time1 and time2 should be in the same format, and the calculation is time1 - time2.

How do you calculate age in a database 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.

How do you compare dates in MySQL?

The DATE() function can be used for the comparison of the date of a number of purposes, some of them are: To extract the specific data on the basis of the date by using the inequality or equality signs. To extract the specific data on the basis of the date between different columns.


1 Answers

You mean like this?

SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(), dob)), "%Y")+0 AS age from sometable

(Source)

like image 117
Nadia Alramli Avatar answered Oct 04 '22 03:10

Nadia Alramli