Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Oracle Year/Month/Day Difference between two columns

Posted on 2013-01-11
4
Medium Priority
?
4,659 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
[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
  • 2
4 Comments
 
LVL 23

Accepted Solution

by:
Steve Wales earned 2000 total points
ID: 38767408
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 74

Expert Comment

by:sdstuber
ID: 38767447
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 74

Expert Comment

by:sdstuber
ID: 38767459
oops, slow, same idea different arrangement
0
 

Author Comment

by:diannagibbs
ID: 38767556
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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
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 explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
Via a live example, show how to take different types of Oracle backups using RMAN.

610 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