1

My date of birth format is : 2017-Feb-15

when I am using the following query, it's not working

select (year(current_date)-year(dob)) as age from user 

or how to current 2017-Feb-15 into 2017-02-15 format

Sarita Sharma
  • 253
  • 2
  • 8
  • 19
  • That's not a very accurate formula for people born on Jan 1st or Dec 31st – Mawg says reinstate Monica Aug 02 '18 at 06:28
  • You may find this link usefully: 'https://stackoverflow.com/questions/10506731/get-difference-in-years-between-two-dates-in-mysql-as-an-integer' In short... Use the datediff(...) mysql function that will give you the difference between to dates. From the results above use the mysql floor(...) function that will return you the largest integer value that is less than or equal to a number. – TechManiac Aug 02 '18 at 06:31
  • Fix your dob format – Strawberry Aug 02 '18 at 06:33
  • @Tech a good link but the best answer is not the accepted one. `TIMESTAMPDIFF` is superior. OP will still need to convert his date format though. – Nick Aug 02 '18 at 06:46

1 Answers1

1

You can use TIMESTAMPDIFF.

select TIMESTAMPDIFF(YEAR, str_to_date(dob, '%Y-%M-%d'), current_date) AS age
from user;
skelwa
  • 575
  • 1
  • 7
  • 17