Build dashboards & reports in minutes

  • Features
  • Pricing
  • Blog
  • What is Ubiq
  • Free Trial

How to calculate age from date of birth in SQL

November 29, 2013September 21, 2021 Sreeram Sreenivasan

Last updated on September 21st, 2021 at 11:11 am

Here’s how to calculate age from date of birth in SQL. You can use the following MySQL query. Just replace date_of_activity and table_name with your column name. date_of_activity is a column in table table_name. This is useful if you need to find out recency of activity.

SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(now()) - TO_DAYS(date_of_activity)), '%Y') + 0 from table_name;

with stored variable

SET @date_of_activity='2013-29-11 10:00:00';
SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(now()) - TO_DAYS(@date_of_activity)), '%Y') + 0 from table_name;

The above statement subtracts the date of activity from latest date. It then converts the result into years.

Some use cases:

Table of Contents

Toggle
  • Calculate age from date of birth of person in years
  • Calculate comment’s age from date of posting a comment
  • Calculate user’s age from date of joining or signup

Calculate age from date of birth of person in years

SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(now()) - TO_DAYS(date_of_birth)), '%Y') + 0 from table_name;

Calculate comment’s age from date of posting a comment

SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(now()) - TO_DAYS(date_of_posting)), '%Y') + 0 from table_name;

Calculate user’s age from date of joining or signup

SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS(now()) - TO_DAYS(date_joined)), '%Y') + 0 from table_name;

Related posts:

Top Free Database Design Tools

Calculate percentile in MySQL based on totals

How To Get Records from Last 24 Hours in MySQL

mm
Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on X (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
MySQL age, age calculation, age from date, age from dob, age in years, age of comment, data, database, date, date format, date format string, date of joining, date of posting, date of signup, mysql, query, sql. permalink.

Post navigation

How to find difference between two dates in MySQL
How to do a bulk insert in MySQL?

About Us



Ubiq is a business intelligence & reporting software. Build business dashboards, charts & reports in minutes. Get insights from data quickly. Try it for free!




Data Reporting
  • MySQL Reporting Tools
  • PostgreSQL Reporting Tools
  • Online Reporting Tools
  • Web Reporting Tools
  • Redshift BI Reporting
  • SQL Reporting Tools
Business Intelligence
  • BI Solution
  • BI Reporting Software
  • MySQL BI Reporting Tools
  • Self Service BI
  • SaaS BI
Data Visualization
  • Data Visualization Tools
  • Data Analysis Tools
  • Visual Analytics
  • MySQL Charts
  • MySQL Graph Generator
  • MySQL Report Builder
  • Online Report Generator
  • Redshift Data Visualization
Dashboards
  • Dashboard Builder
  • Dashboard Reporting Software
  • Dashboard Creator
  • KPI Dashboard Software
Quicklinks
  • Contact Us
  • Docs
  • Jobs
  • BI Blog
  • Database Blog
  • Tech Blog
Resources
  • Security
  • Privacy
  • T&C
  • Sitemap
dazzling Theme by Colorlib Powered by WordPress