Date functions – months_between

The Oracle/PLSQL date function MONTHS_BETWEEN returns the number of months between two dates that are passed as arguments.

Syntax:

MONTHS_BETWEEN(date1, date2)
 
Example code:
 
<pre lang="plsql">SELECT TRUNC(SYSDATE), MONTHS_BETWEEN(SYSDATE, '31-JUL-2009')
  FROM dual

trunc(sysdate) months_between
———————————–
12/31/2009 5

SELECT TRUNC(SYSDATE), MONTHS_BETWEEN(SYSDATE, '11-FEB-2009')
  FROM dual

trunc(sysdate) months_between
——————————————–
12/31/2009 10.6481179062127

This return fraction of month. trunc can be used to get the whole number as shown in the following code

SELECT trunk(SYSDATE), TRUNC(MONTHS_BETWEEN(SYSDATE, '11-FEB-2009'))
  FROM dual

trunc(sysdate) months_between
——————————————-
12/31/2009 10

Share Article/Example:
  • DotNetKicks
  • DZone
  • StumbleUpon
  • Print
  • Add to favorites
  • Digg
  • del.icio.us
  • Twitter
  • Facebook
  • LinkedIn
  • Posterous
  • Slashdot

 

 
eXTReMe Tracker