Solved

Month difference between dates in Pl/SQL

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

839 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