MONTHS_BETWEEN () Function

Using this method in PL/SQL you can calculate the number of months between two entered dates date1 and date2. if date1 is later than date2 then the result would be positive and if date1 is earlier than date2 then result is negative.

Note: If a fractional month is calculated, the MONTHS_BETWEEN function calculates the fraction based on a 31-day month.

MONTHS_BETWEEN () Function Example

Query :

SELECT MONTHS_BETWEEN (TO_DATE ('01-07-2003', 'dd-mm-yyyy'),
TO_DATE ('14-03-2003', 'dd-mm-yyyy')) AS NUMBER_OF_MONTHS
FROM Dual
SQL
SELECT MONTHS_BETWEEN (TO_DATE ('01-07-2003', 'dd-mm-yyyy'), 
                       TO_DATE ('14-03-2003', 'dd-mm-yyyy')) AS NUMBER_OF_MONTHS
FROM Dual

Output:

NUMBER_OF_MONTHS
3.58

Explanation: Here date1 and date2 are not on the same day of the month that’s why we are getting the value in fractions, as well as date1 is later than date2 so the resulting value is in integers. Entered date should be in particular date format, that is the reason of using TO_DATE function while comparison within MONTHS_BETWEEN function. Let’s select the number of months an employee has worked for the company.

Date and Time Functions in PL/SQL

Date and Time Functions in PL/SQL are useful for date and time operations like extracting date components, performing date arithmetic, and converting between date formats.

Similar Reads

Date and Time Functions in PL/SQL

There are many useful Date and Time functions in PL/SQL. A table of such functions along with their description is given below:...

SYSDATE Function

The SQL SYSDATE function returns the current date and time set for operating system on which the database resides....

EXTRACT() Function

Oracle helps you to extract Year, Month, and Day from a date using the Extract() Function....

ADD_MONTHS () Function

Using this method in PL/SQL you can add as well as subtract a number of months(n) to a date. Here ‘n’ can be both negative or positive....

LAST_DAY() Function

Using this method in PL/SQL you can get the last day in the month of specified date....

MONTHS_BETWEEN () Function

Using this method in PL/SQL you can calculate the number of months between two entered dates date1 and date2. if date1 is later than date2 then the result would be positive and if date1 is earlier than date2 then result is negative....

NEXT_DAY() Function

It will return the upcoming date of the first weekday that is later than the entered date.It has two parameters first date where, system date or specified date can be entered; second day of week which should be in character form....

Contact Us