Calculate age in MySQL (InnoDB)
If someone's date of birth is stored as a string as 'dd-mm-yyyy', subtracting it from the current date using the DATE() or CURDATE() function will return 'YYYY-MM-DD' format results.
To extract the age from this result, you can use the TIMESTAMPDIFF() function with YEAR units:
<code class="language-sql">SELECT TIMESTAMPDIFF(YEAR, DATE('1970-02-01'), CURDATE()) AS age;</code>
In this example, '1970-02-01' represents the person's date of birth. The resulting age will be the number of complete years that have passed since their birth date.
Demo
<code class="language-sql">SELECT TIMESTAMPDIFF(YEAR, '1970-02-01', '2023-05-01') AS age;</code>
Output:
<code>age 53</code>
This demonstrates how to use TIMESTAMPDIFF() to calculate age based on a stored date of birth and the current date.
The above is the detailed content of How Can I Calculate Age from a Date of Birth Stored as a String in MySQL?. For more information, please follow other related articles on the PHP Chinese website!