Total Pageviews

2020/10/03

[Oracle] months_between


若我想知道兩個日期間差幾個月,可運用 months_between 此內建 function 來完成,若要做四捨五入,可加上 round function;若要做無條件進位,可加上 ceil function。

範例如下:
select months_between(to_date('20200901', 'YYYYMMDD'), TO_DATE('20200722', 'YYYYMMDD'))        as VAL1,
       round(months_between(to_date('20200901', 'YYYYMMDD'), TO_DATE('20200722', 'YYYYMMDD'))) as VAL2,
       ceil(months_between(to_date('20200901', 'YYYYMMDD'), TO_DATE('20200722', 'YYYYMMDD')))  as VAL3
from dual;

執行結果如下:









Reference

No comments: