+ 1

Sql

I want to include a constraint dateOfBirth that it should be greater than 21 using sql oracle please help. In a way today's date should be greater than the dateOfBirth with 21 or more. Please help

16th Apr 2024, 12:02 PM
Atlang Maphakela
Atlang Maphakela - avatar
1 Resposta
+ 6
To find age, use DATEDIFF(NOW(), dateOfBirth). Put that into a WHERE clause to filter ages >=21. EDIT: I apologize that somehow I misinterpreted which SQL engine you were asking about. Oracle is different. In Oracle you can get days between dates by mere subtraction, (SYSDATE - dateOfBirth). Years can be calculated by dividing by days in a year which is precisely 365.2425. Compare that with 21 in the WHERE clause. This method is a fair approximation within a few days but it might not be perfectly in phase to actual leap years. There are more elaborate ideas on Stack Overflow. https://stackoverflow.com/questions/3015431/oracle-age-calculation-from-date-of-birth-and-today
16th Apr 2024, 1:21 PM
Brian
Brian - avatar