Recently, I was working on a project in which I had to write a SQL query for getting age in years and then filter the records based on the age. In the underlying database, I had a table that stored the date of birth of people along with other details. In this example, I have explained how to calculate age from date of birth in SQL.
Database Structure
For demonstration purposes, I have created a sample database that consists of a table with some dummy records. The queries for creating the sample database are given below:
Once the database structure has been created, we can write a SQL query for getting age in years as follows:
This query yields a result set as shown below:
You can also extend the query using aggregate functions and clauses like:
The output of the above query would be:
I hope you have understood how to calculate age from date of birth in SQL. In case you have any doubts, please ask me questions in the comments section below. If you want to learn more, have a look at the post Display a Confirmation Dialog from Server Side in ASP.NET which explains a very useful trick in ASP.NET.