Solved

Oracle Year/Month/Day Difference between two columns

Posted on 2013-01-11
4
3,696 Views
Last Modified: 2013-01-11
I found this SQL on the web - thanks to whomever wrote it.  But as you can see from my dates, it's not correctly working.  Is it because of leap years?  Any thoughts, comments, direction greatly appreciated.  Thanks.


select ID, BIRTH, MEETING_DATE,
trunc(months_between(MEETING_DATE,BIRTH)/12) year,
    trunc(mod(months_between(MEETING_DATE,BIRTH),12)) month,
   trunc(sysdate-add_months(BIRTH,trunc(months_between(MEETING_DATE,BIRTH)/12)*12+trunc(mod(months_between(MEETING_DATE,BIRTH),12)))) day
   from ica_data
/



ID           BIRTH     MEETING_DATE     YEAR      MONTH        DAY
------------------------- --------- --------- ---------- ---------- ----------
8900007      01-JAN-08 29-NOV-12          4         10         71
9560033      11-DEC-11 29-NOV-12          0         11         61
9570904      29-MAR-12 31-OCT-12          0          7         74
9560033      11-DEC-11 29-NOV-12          0         11         61
9560033      11-DEC-11 17-DEC-12          1          0         31
0
Comment
Question by:diannagibbs
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
Steve Wales earned 500 total points
Comment Utility
When you're calculating "DAYS", why are you using sysdate as your beginning date for the calculation ?

Try this maybe?

select ID, BIRTH, MEETING_DATE,
trunc(months_between(MEETING_DATE,BIRTH)/12) year,
    trunc(mod(months_between(MEETING_DATE,BIRTH),12)) month,
   trunc(MEETING_DATE-(add_months(BIRTH,trunc(months_between(MEETING_DATE,BIRTH)/12)*12+trunc(mod(months_between(MEETING_DATE,BIRTH),12))))) day
   from ica_data
/

I ran the query against your data and get this (which looks better):

        ID BIRTH     MEETING_D       YEAR      MONTH        DAY
---------- --------- --------- ---------- ---------- ----------
   8900007 01-JAN-08 29-NOV-12          4         10         28
   9560033 11-DEC-11 29-NOV-12          0         11         18
   9570904 29-MAR-12 31-OCT-12          0          7          2
   9560033 17-DEC-11 17-DEC-12          1          0          0

Open in new window

0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
you're using sysdate where you should be using meeting_date
here's a reorganized version that uses the same calculations but hopefully a little easier to read


select id, birth,meeting_date,
       year,month, meeting_date - add_months(birth,12*year+month) day
from (SELECT id,
       birth,
       meeting_date,
       TRUNC(MONTHS_BETWEEN(meeting_date, birth) / 12) year,
       TRUNC(MOD(MONTHS_BETWEEN(meeting_date, birth), 12)) month
  FROM ica_data)
0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
oops, slow, same idea different arrangement
0
 

Author Comment

by:diannagibbs
Comment Utility
Oversite on my part - forgot to change that to the meeting date instead of sysdate.  Thanks so much to you both.  Exactly what I was looking for.  Appreciate fast respone.
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

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
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…
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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

762 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

6 Experts available now in Live!

Get 1:1 Help Now