Solved

Month difference between dates in Pl/SQL

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

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Stay Alert! 13 62
percentage remaining 6 40
Value of 0's not appearing. 9 50
report returning null 21 52
Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

757 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

21 Experts available now in Live!

Get 1:1 Help Now