Solved

Month difference between dates in Pl/SQL

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
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 explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

707 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