clock  Mon - Sun 8.00 AM - 8.00 PM
mail  edu@teknowize.com
fb
instagram
play store
pinterest

How to calculate age from date of birth column using mysql

  Hariom Prajapati
  24 Jun 2022
  MySQL
Imperial Advance Hero Image

How to calculate age from date of birth column using mysql

In this tutorial we will see that, how to calculate age from date of birth column using mysql.

Many time we need to calculate age from date of birth, so we can do it using php, Laravel, codeigniter etc but here we will see how to calculate age from date of birth column using SQL Server DATEDIFF() Function.

 

Syntax –

DATEDIFF(intervaldate1date2)

 

 Parameter values -

Parameter

Description

interval

Required. The part to return. Can be one of the following values:

  • year, yyyy, yy = Year
  • quarter, qq, q = Quarter
  • month, mm, m = month
  • dayofyear = Day of the year
  • day, dy, y = Day
  • week, ww, wk = Week
  • weekday, dw, w = Weekday
  • hour, hh = hour
  • minute, mi, n = Minute
  • second, ss, s = Second
  • millisecond, ms = Millisecond

date1, date2

Required. The two dates to calculate the difference between

 

Example for get age from date of birth column –

SELECT 
           Id,
            (DATEDIFF(CURRENT_DATE,
STR_TO_DATE(born_date, ' %Y-%m-%d'))/365) as age FROM `users`