Calculate age: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
(Created page with "Calculate age in Excel or MySQL == In Excel == If the column value is well-formatted * {{kbd | key=<nowiki>=(TODAY()- BIRTH_DAY)/365</nowiki>}}<ref>[https://www.ablebits.com...")
 
mNo edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
Calculate age in Excel or MySQL
Calculate age in Excel or MySQL


== In Excel ==
== Calculate age in Excel ==
If the column value is well-formatted
If the column value is well-formatted


Line 7: Line 7:
* {{kbd | key=<nowiki>=ROUNDDOWN(YEARFRAC(BIRTH_DAY, TODAY(), 1), 0)</nowiki>}}
* {{kbd | key=<nowiki>=ROUNDDOWN(YEARFRAC(BIRTH_DAY, TODAY(), 1), 0)</nowiki>}}


== In MySQL ==
== Calculate age in MySQL ==


<pre>
<pre>
Line 16: Line 16:


<ref>[https://stackoverflow.com/questions/5773405/calculate-age-in-mysql-innodb sql - Calculate Age in MySQL (InnoDb) - Stack Overflow]</ref>
<ref>[https://stackoverflow.com/questions/5773405/calculate-age-in-mysql-innodb sql - Calculate Age in MySQL (InnoDb) - Stack Overflow]</ref>
== Challenge of real world data ==
The column value is '''NOT''' well-formatted such as
* year only e.g. {{CURRENTYEAR}}
* month & date only e.g. {{CURRENTMONTH}}-{{CURRENTDAY}}


== References ==
== References ==
<references />
<references />


[[Category:Excel]] [[Category:MySQL]]
[[Category:Excel]] [[Category:MySQL]] [[Category:Time river]]

Latest revision as of 14:01, 10 February 2022

Calculate age in Excel or MySQL

Calculate age in Excel[edit]

If the column value is well-formatted

  • =(TODAY()- BIRTH_DAY)/365[1]
  • =ROUNDDOWN(YEARFRAC(BIRTH_DAY, TODAY(), 1), 0)

Calculate age in MySQL[edit]

SELECT `birth_date`, TIMESTAMPDIFF (YEAR, `birth_date`, CURDATE()) AS age
FROM `my_table` 
WHERE `birth_date` IS NOT NULL

[2]

Challenge of real world data[edit]

The column value is NOT well-formatted such as

  • year only e.g. 2026
  • month & date only e.g. 04-18

References[edit]