在 Oracle 中,我需要计算自给定日期以来经过的年数从 1 开始的值,考虑闰年。
最初的计算没有考虑闰年。这是
CEIL ((SYSDATE- prog_start_dt) / 365))
然后我决定使用MONTHS_BETWEEN
,但我的新计算中有一个错误:
CEIL(MONTHS_BETWEEN(SYSDATE, prog_start_dt) / 12)
问题是,如果我正好在 prog_start_date
之后 1 年,无论时间如何,该值都会少 1。例如:
sysdate = 2020-07-01 15:30:00
prog_start_dt = 2019-07-01 00:00:00
--> 1. CEIL ((SYSDATE- prog_start_dt) / 365)) --> Result: 2
--> 2. CEIL(MONTHS_BETWEEN(SYSDATE, prog_start_dt) / 12) --> Result 1
正确的结果是2
。因此,我需要顶部公式的行为,但要考虑闰年,这显然不会是 365
。有什么好的解决办法吗?
最佳答案
If
date1
anddate2
are either the same days of the month or both last days of months, then the result is always an integer.
如果您确实想解决这个问题,那么一个选择是添加一些逻辑来处理这种特定情况:当比较的日期属于同一月份和同一天时,然后检查时间部分以查看年份是否已到已过去:
ceil(months_between(sysdate, prog_start_dt) / 12)
+ case when to_char(sysdate, 'mm-dd') = to_char(prog_start_dt, 'mm-dd')
and to_char(sysdate, 'hh24:mi') > to_char(prog_start_dt, 'hh24:mi')
then 1
else 0
end
<强> Demo on DB Fiddle :
with t as (
select
to_date('2020-07-01 15:30:00', 'yyyy-mm-dd hh24:mi:ss') date1,
to_date('2019-07-01 00:00:00' , 'yyyy-mm-dd hh24:mi:ss') date2
from dual
)
select
ceil(months_between(date1, date2) / 12)
+ case when to_char(date1, 'mm-dd') = to_char(date2, 'mm-dd')
and to_char(date1, 'hh24:mi') > to_char(date2, 'hh24:mi')
then 1
else 0
end year_diff
from t
| YEAR_DIFF | | --------: | | 2 |
关于sql - 在 Oracle 中准确计算过去的年数(考虑闰年),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62686885/