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?
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?
Student
The Anubhav portal was launched in March 2015 at the behest of the Hon'ble Prime Minister for retiring government officials to leave a record of their experiences while in Govt service .
To calculate age from date of birth in SQL Server, you can use the
DATEDIFFfunction 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
Userswith columnsUserIDandDateOfBirth. 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