Hi guys, had a collum brith_day and hire_ date ,i need to show empolees who had 45 yers at the moment of hire
i try to (birth_date-hire_date) = 45 but dosnt work, can someone help me? still learn it
Loading
Hi guys, had a collum brith_day and hire_ date ,i need to show empolees who had 45 yers at the moment of hire
i try to (birth_date-hire_date) = 45 but dosnt work, can someone help me? still learn it
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Jignesh KumarRank #91 · 22.9K reputationPosted Dec 4, 2023, 1:29 AM EST
Hello,
You can find using DateDiff() function,
SELECT employee_id, date_of_birth, date_of_hire FROM employees WHERE DATEDIFF(YEAR, date_of_birth, date_of_hire) = 45;Jayraj ChhayaRank #304 · 8.2K reputationPosted Dec 3, 2023, 11:24 AM EST
To find employees who were 45 years old at the time of their hire, you need to consider the date difference between their birth date and hire date. However, directly subtracting the birth date from the hire date will not yield the desired result.
In SQL, you can use the
DATEDIFF()function to calculate the age of an employee at the time of hire. TheDATEDIFF()function calculates the difference between two dates in a specified unit (e.g., years, months, days).SELECT * FROM employees WHERE DATEDIFF(YEAR, birth_date, hire_date) = 45;In this query, we are using the
DATEDIFF()function to calculate the difference in years between thebirth_dateandhire_date. We then compare this difference to 45 to filter out employees who were exactly 45 years old at the time of their hire.Make sure to replace
employeeswith the actual name of your table. Additionally, ensure that the column namesbirth_dateandhire_datematch the column names in your table.By using the
DATEDIFF()function in this way, you should be able to retrieve the employees who satisfy the condition of being 45 years old at the time of their hire.