0

I'm trying to show my members age in profile page. I found 2 SQL queries:

SELECT TIMESTAMPDIFF(YEAR, dob, CURDATE()) AS age FROM users

and

SELECT FLOOR(DATEDIFF(CURRENT_DATE(), dob) / 365)

What SQL query better?? (For performance, accurate etc.)

Bob Smith
  • 63
  • 2
  • 5
  • 2
    There is more that 365 days in a year. That is why we have leap years. – Ed Heal Jan 18 '14 at 22:53
  • 2
    possible duplicate of [Calculate Age in MySQL (InnoDb)](http://stackoverflow.com/questions/5773405/calculate-age-in-mysql-innodb) – O. Jones Jan 18 '14 at 22:59

1 Answers1

0

I am not sure how this will translate into mysql. But here is a T-SQL that can calculate age for a person, given their birth date and current date.

select case 
            when cast(getdate() as date) = cast(dateadd(year, (datediff(year, '1996-09-09', getdate())), '1996-09-09') as date)
                then dateDiff(yyyy,'1996-09-09',dateadd(year, 0, getdate()))
            else dateDiff(yyyy,'1996-09-09',dateadd(year, -1, getdate()))
        end as MemberAge
go
Komengem
  • 3,662
  • 7
  • 33
  • 57