?
Solved

Month difference between dates in Pl/SQL

Posted on 2003-12-06
5
Medium Priority
?
10,552 Views
Last Modified: 2013-12-12
Hello,

I need to get such difference in months. Example: if date1 is 2003.10.01 and date2 is 2003.10.01 i need to get difference 1.
And if date1 is 2003.07.01 and date2 is 2003.09.01 i need to get difference in months equal 3.
How could i write it?
Thanks.
0
Comment
Question by:sky_lt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 5

Expert Comment

by:gmyers
ID: 9893512
v_months_diff = 1 + ((to_number(to_char(v_date2,'YYYY')) * 12 ) +to_number(to_char(v_date2,'MM'))) -
                          ((to_number(to_char(v_date1,'YYYY')) * 12 ) +to_number(to_char(v_date1,'MM')))
0
 
LVL 2

Accepted Solution

by:
ypwitkow earned 200 total points
ID: 9895284
Try :

SELECT months_between(date2,date1) + 1 INTO v_months FROM dual;

Greetings,
Lucy
0
 
LVL 2

Expert Comment

by:alligatorsql
ID: 9897919
Hello,

I prefer the following statement

SELECT ABS(MONTHS_BETWEEN(TO_DATE('2003.10.01','yyyy.mm.dd'), TO_DATE('2003.11.01','yyyy.mm.dd')))  FROM dual

With that you get the real difference between both dates and it doesn´t matter
wether date1 is greater date2 or not.

Best regards
Manfred Peter
0
 
LVL 22

Expert Comment

by:Helena Marková
ID: 10207250
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept ypwitkow's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Henka
EE Cleanup Volunteer
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question