Skip to content
Advertisement

MySQL calculating age based on birthDate

I am trying to calculate the age based on the birthDate in MySQL using SQL statement.

My birthDate was varchar() and in this format: 29/11/1994 (DD/MM/YYYY).

My SQL statement:

SELECT DATEDIFF(YY, birthDate, GetDate()) AS Age
FROM bookedevent be INNER JOIN account a
ON be.bookedEventBY = a.accountName WHERE a.accountID = 1

However, when I test in MySQL workbench, I am getting this error message: Incorrect parameter count in the call DATEDIFF.

ANy guides?

Thanks in advance.

Advertisement

Answer

You are using SQL Server syntax in MySQL. That won’t work. You can use TIMESTAMPDIFF():

SELECT TIMESTAMPDIFF(YEAR, birthDate, CURDATE()) AS Age
FROM bookedevent be INNER JOIN
     account a
     ON be.bookedEventBY = a.accountName
WHERE a.accountID = 1;

Note that the semantics for TIMESTAMPDIFF() are different than for DATEDIFF() in SQL Server. However, TIMESTAMPDIFF() is probably closer to what you really want.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement