To convert the date of DOB to age in SQL, we can use the DATEDIFF function. The DATEDIFF function calculates the difference between two dates, expressed in a specified date part, such as years, months, or days.
To convert the date of birth to age, you would subtract the date of birth from the current date or a specified date, and express the difference in years.
Here is an example of how to convert the date of birth to age in SQL using the DATEDIFF function:
SELECT name, DATEDIFF(YEAR, date_of_birth, GETDATE()) AS age
FROM employees;
In this example, name and date_of_birth are columns in the employee's table, and GETDATE() returns the current date.
The DATEDIFF function calculates the difference in years between the date_of_birth and the current date, and the calculation result is stored in a new column age. This query returns the name and age of each employee in the employees' table. Read more...