You can try months_between . It will calculate the number of months between two dates as a decimal number.
select months_between(sysdate+30, sysdate ) from dual; select months_between(sysdate+15, sysdate ) from dual;
In this example, the first parameter is larger than the second, so it will return 1. The second line returns ~ 0.48 (when executed at 11:30 on 2010-09-01) To get the actual date values:
select case when months_between(sysdate+30, sysdate ) > 0 then sysdate+30 else sysdate end from dual;
Generally:
case when months_between(dateA, dateB ) > 0 then dateA else dateB
Update:
After some experimentation, it seems that the best graininess of this function is Day.
select months_between(to_date('2010-10-16 23:59:59', 'YYYY-MM-DD HH24:MI:SS'), to_date('2010-10-16 00:00:00', 'YYYY-MM-DD HH24:MI:SS')) from dual;
... will return 0
but
select months_between(to_date('2010-10-17 00:00:00', 'YYYY-MM-DD HH24:MI:SS'), to_date('2010-10-16 00:00:00', 'YYYY-MM-DD HH24:MI:SS')) from dual;
will return 0.032258064516129.
Some other interesting date difference / compare methods here: http://www.orafaq.com/faq/how_does_one_get_the_time_difference_between_two_date_columns
FrustratedWithFormsDesigner
source share