我在查找oracle
函數或過程,根據Input Date
和Frequency(Yearly,half yearly, Quarterly)
計算下一個日期。PL/SQL - 基於頻率的下一個日期
例如
Input Date Frequency Next Occurrence Date
1-Jan-2016 Quarterly 1-Apr-2016
1-Jan-2016 yearly 1-Jan-2017
我在查找oracle
函數或過程,根據Input Date
和Frequency(Yearly,half yearly, Quarterly)
計算下一個日期。PL/SQL - 基於頻率的下一個日期
例如
Input Date Frequency Next Occurrence Date
1-Jan-2016 Quarterly 1-Apr-2016
1-Jan-2016 yearly 1-Jan-2017
考慮Oracle的功能ADD_MONTHS(date, number_months)
獲得每月,每季度,每年的結果。
例如,下一季度將是3個月。因此,如果輸入日期是第一季度,您可以使用:
ADD_MONTHS(01-JAN-2016, 3)
將獲得下一季度。
例如,SQL:
select TO_DATE('01-JAN-2016', 'DD-MON-YYYY') as "Input Date",
'Quarterly' as "Frequency",
ADD_MONTHS(TO_DATE('01-JAN-2016', 'DD-MON-YYYY'), 3) as "Next Occurrence Date"
from dual
union
select TO_DATE('01-JAN-2016', 'DD-MON-YYYY') as "Input Date",
'Yearly' as "Frequency",
ADD_MONTHS(TO_DATE('01-JAN-2016', 'DD-MON-YYYY'), 12) as "Next Occurrence Date"
from dual
;
結果:
Input Date Frequency Next Occurrence Date
------------------ --------- ------------------
01-JAN-16 Quarterly 01-APR-16
01-JAN-16 Yearly 01-JAN-17
擴展@ tale852150的回答一點,你可以使用的情況下,體現出了頻率轉換成個月增加的數量,假設你有已知的可能值列表:
add_months(input_date, case frequency
when 'Quarterly' then 3
when 'Half yearly' then 6
when 'Yearly' then 12 else 0 end
快速演示:
with t (input_date, frequency) as (
select date '2016-01-01', 'Quarterly' from dual
union all select date '2016-01-01', 'Half yearly' from dual
union all select date '2016-01-01', 'Yearly' from dual
)
select input_date, frequency,
add_months(input_date, case frequency
when 'Quarterly' then 3
when 'Half yearly' then 6
when 'Yearly' then 12 else 0 end) as next_occurrence_date
from t;
INPUT_DATE FREQUENCY NEXT_OCCURRENCE_DATE
---------- ----------- --------------------
2016-01-01 Quarterly 2016-04-01
2016-01-01 Half yearly 2016-07-01
2016-01-01 Yearly 2017-01-01