I'm trying to calculate the number of years, months and days between sysdate and hire_date column considering 28,29,30 and 31 months
i've managed to calculate both the years and months yet i need your help in calculating the days.
Here's the code so far.
`
SELECT employee_id, last_name, hire_date,
trunc( months_between(sysdate, hire_date) / 12 ) as years,
trunc(mod( months_between(sysdate, hire_date) , 12 )) as remaining_months
FROM employees;
`
Thanks in advance.
I've calculated the years and months and i need help in calculating the days and possibly how to calculate it.
You can add the number of full months/years that you have already calculated to the hire_date
and then find the number of days from that to the current date:
SELECT employee_id,
last_name,
hire_date,
trunc(months_between(sysdate, hire_date) / 12) as years,
trunc(mod(months_between(sysdate, hire_date) , 12)) as remaining_months,
TRUNC(SYSDATE) - ADD_MONTHS(hire_date, trunc(months_between(sysdate, hire_date)))
AS remaining_days
FROM employees;
Which, for the sample data:
CREATE TABLE employees (employee_id, last_name, hire_date) AS
SELECT 1, 'Alice', DATE '2022-01-01' FROM DUAL UNION ALL
SELECT 2, 'Beryl', DATE '2020-11-30' FROM DUAL UNION ALL
SELECT 3, 'Carol', DATE '2000-04-13' FROM DUAL;
Outputs:
EMPLOYEE_ID | LAST_NAME | HIRE_DATE | YEARS | REMAINING_MONTHS | REMAINING_DAYS |
---|---|---|---|---|---|
1 | Alice | 01-JAN-22 | 0 | 11 | 12 |
2 | Beryl | 30-NOV-20 | 2 | 0 | 13 |
3 | Carol | 13-APR-00 | 22 | 8 | 0 |
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.