We have employees of all ages in the database. We need to select only those employees whose age is more than 25 years 2 months.
Asked
Active
Viewed 256 times
3 Answers
0
this will work:
SELECT * FROM emp where sysdate-birthdate>=(SELECT sysdate-
to_date('19931015','YYYYMMDD') FROM dual);
nikhil sugandh
- 3,422
- 4
- 16
- 29
0
you could use the months_between function https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions089.htm
Oracles own example:
SELECT MONTHS_BETWEEN (TO_DATE('02-02-1995','MM-DD-YYYY'),
TO_DATE('01-01-1995','MM-DD-YYYY') ) "Months"
FROM DUAL;
Months
1.03225806
your query would be something like
select * from emp where MONTHS_BETWEEN (sysdate, date_of_birth) > ((25*12)+2)
Littlefoot
- 107,599
- 14
- 32
- 52
Kristian Saksen
- 94
- 5
0
ADD_MONTHS might help, e.g.
select *
from employees
where date_of_birth > add_months(trunc(sysdate), -(25 * 12 + 2));
ADD_MONTHSadds number of months to the first parameterTRUNC(SYSDATE)returns today's day at midnight; dates of birth usually don't have a time component.- the second parameter is:
-, because we have to subtract months ("25 years ago")25- 25 years12- a year has 12 months+ 2add 2 months
Littlefoot
- 107,599
- 14
- 32
- 52