[英]How to calculate the number of days dynamically (Oracle SQL)
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考虑到 28、29、30 和 31 个月,我正在尝试计算 sysdate 和 hire_date 列之间的年数、月数和天数
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:您可以将已计算的完整月/年数添加到hire_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员工ID | LAST_NAME姓 | HIRE_DATE雇用日期 | YEARS年 | REMAINING_MONTHS REMAINING_MONTHS | REMAINING_DAYS REMAINING_DAYS 天 |
---|---|---|---|---|---|
1 1个 | Alice爱丽丝 | 01-JAN-22 22 年 1 月 1 日 | 0 0 | 11 11 | 12 12 |
2 2个 | Beryl绿柱石 | 30-NOV-20 2020 年 11 月 30 日 | 2 2个 | 0 0 | 13 13 |
3 3个 | Carol颂歌 | 13-APR-00 2000 年 4 月 13 日 | 22 22 | 8 8个 | 0 0 |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.