in Web and Tech, Work

SQL query to capture age

Age is one of the many common derivative fields one can come across in your career as a developer. Personally, I’ve encountered this so many times. And yet, I can’t seem to remember how exactly I did it before. I end up either trying to reformulate, do google search, or browse for my old code. So, in the interest of ease and comfort, I’m posting this snippet of code here for quick and easy reference. 😀

SELECT DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(dob, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(dob, '00-%m-%d')) AS age

Write a Comment

Comment