Solved

Month difference between dates in Pl/SQL

Posted on 2003-12-06
5
10,533 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
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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help with Oracle syntax 4 56
Dataware house query tuning 9 64
selective queries 7 21
ORA-00923: FROM keyword not found where expected 3 30
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to recover a database from a user managed backup

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now