0

I am using MySql 5.7.18-log.

I have a query as follows, which result name and date of birth in output.

select uid,Name, Dob from table1;

Data:

uid       Name          Dob
K-1       Alex          1996-01-01 05:30:00
K-2       Mike          1983-05-26 05:30:00
K-3       Jim           1992-12-29 05:30:00

I want to extract age using the year and month of Dob column and extracting it from the current year and month.

Required output:

uid       Name          Dob                    Age
K-1       Alex          1996-01-01 05:30:00    25
K-2       Mike          1983-05-26 05:30:00    37
K-3       Jim           1992-12-29 05:30:00    28
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
Sophia Wilson
  • 581
  • 3
  • 16

1 Answers1

2

This is where TIMESTAMPDIFF() is your friend:

SELECT t.`uid`, t.`Name`, t.`Dob`,
       TIMESTAMPDIFF(YEAR, t.`Dob`, CURDATE()) AS age
  FROM `table1` t

This will always return a whole number rounded down to the nearest integer.

Matigo
  • 169
  • 5