Hello,
I need a query to calculate the age of employees from their DateOfBirth
column in the Employee
table. How can I do this in SQL Server?
Home / DeveloperSection / Forums / SQL Query to Calculate Age from Date of Birth in SQL Server
Hello,
I need a query to calculate the age of employees from their DateOfBirth
column in the Employee
table. How can I do this in SQL Server?
Ravi Vishwakarma
16-Jul-2024To calculate age from date of birth in SQL Server, you can use the
DATEDIFF
function along withGETDATE()
to get the current date.Here's an example query:
Example 1
DATEDIFF(YEAR, DateOfBirth, GETDATE()): Calculates the difference in years between the
current date (
GETDATE()
) and theDateOfBirth
.Example 2
CASE ... END: Adjusts the age if the birth date for this year hasn't occurred yet. It subtracts 1 from the age if the birthday hasn't occurred in the current year.
This query assumes you have a table named
Users
with columnsUserID
andDateOfBirth
. Adjust the table and column names as per your database schema.How do I use CTE to simplify complex queries in SQL Server?
Explain the Aggregate functions in the SQL server.
Explain the transaction in SQL Server.
MERGE statement in SQL Server to perform upserts