How To Calculate Difference Between Dates in Oracle
How To Calculate Difference Between Dates in Oracle
1 of 7
RSS
Subscribe
Get Linked
Google
Follow Us
(http://feeds.feedburner.com
(http://www.linkedin.com
(https://plus.google.com
/oracletutsnetwork)
/u/0/110990718337941106533)
/in/tabrahamsen)
Follow Us
Follow Us
Youtube
Follow Us
Posted February 26th, 2012 by T J Abrahamsen (http://oracletuts.net/author/teab/) & filed under Tutorials (http://ora-
(http://www.facebook.com (http://twitter.com
(https://youtube.com
cletuts.net/category/tutorials/).
/oracletuts)
/user/oracletuts)
/oracletuts)
It can be for example to calculate how long a person has been hired in your company,
how old a person is today, etc.
Here (http://oracletuts.net
/advertise
In this tutorial we are going to look at some ways to do this in Oracle SQL.
Social Media
Recommend on Google
Before we move on
Before we dive into some of the different ways to find the differences between two dates, I just wanted to let you
know that there are many ways to do this. In this tutorial we are going to cover only a few of them.
Also, there are different ways to look at things What I mean is: What if you have two Oracle dates, and you
wanted to come up with a query that shows the months between them. Would you then show the difference as i.e 2.7
months, or do you want to say that there are two whole months between? Or, do you want to show the number of
months involved?
And, do you want to know i.e. just the months between the dates, or do you want to show how many years there
are between the dates too?
This seems pretty elementary, but I would suggest that before you actually start using the different Oracle date functions, you ask yourself exactly what result you would want first. If you do not know up front what your result should
be presented, you might end up with some complications later, since some of the arithmetic can be tricky to tweak afterwards.
Advertise
/advertise
/?instructions=highlightzone837
Ok, before we look at the first example I also would like to mention: Remember the time part of your date field, if it
has one. Many times an order date in an order table will have a date with a time on it. Depending on what method you
are using to calculate any differences between your two dates, you might end up with the wrong result.
Let me explain.
Recent Comments
In the example above you see that if we do a TRUNC on a date that has a time part on it, you will only see the date
part of your date field. If you show you show the time part of a date value that you have used TRUNC on, you will get
a time part of 12:00:00 AM. All dates that do not have a time part specified will by Oracle be thought about as midnight (12:00:00 AM) that day.
1 SQL> SELECT TO_DATE('20120125 11:00:00 AM', 'YYYYMMDD HH:MI:SS AM')
2
2
- TO_DATE('20120120 11:00:00 AM', 'YYYYMMDD HH:MI:SS AM') day_diff
3
3 FROM
dual
4
4 /
5
6
DAY_DIFF
ALI
{ I WANT TO SOLVE MY SOME PROBLEMS IN
ORACLE BY EASY STEPS } Oct 10, 10:13 AM
(http://oracletuts.net/tutorials/introduction-to-aggregatefunctions-in-oracle/#comment-113)
TJ Abrahamsen
{ Hey Vinit - I do not know exactly how
to do that from the top of my head, but you might take a
look at... } Sep 28, 10:06 AM (http://oracletuts.net
/tutorials/how-to-get-elapsed-time-in-plsql/#comment-105)
Vinit
{ Hi, Nice article.. but i need one help. My
requirement is as below: I am calling a procedure P1 in a
package and that procedure... } Sep 26, 10:20 AM
(http://oracletuts.net/tutorials/how-to-get-elapsed-time-inplsql/#comment-99)
Bari
{ Good work, Thanks for explaining transpose
query,have been searching for this one for a while. } Sep
11, 3:38 AM (http://oracletuts.net/tutorials/three-ways-to-
26/10/2012 9:49 AM
2 of 7
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
---------5
SQL> SELECT TO_DATE('20120125 10:00:00 AM', 'YYYYMMDD HH:MI:SS AM')
2
- TO_DATE('20120120 11:00:00 AM', 'YYYYMMDD HH:MI:SS AM') day_diff
3 FROM
dual
4 /
DAY_DIFF
---------4.95833333
SQL> SELECT TO_DATE('20120125 11:00:00 AM', 'YYYYMMDD HH:MI:SS AM')
2
- TO_DATE('20120120 10:00:00 AM', 'YYYYMMDD HH:MI:SS AM') day_diff
3 FROM
dual
4 /
transpose-rows-into-columns-in-oracle-sql/#comment-94)
TJ Abrahamsen
{ Hey Rashed.
I have gotten this
question several times, and every time I seem to answer
that as far as I know, the only... } Aug 14, 9:39 PM
(http://oracletuts.net/tutorials/three-ways-to-transposerows-into-columns-in-oracle-sql/#comment-90)
(http://oracletuts.net/tutorials/how-to-calculate-differencebetween-dates-in-oracle-sql/?bwprc_paged=2)
DAY_DIFF
---------5.04166666
If you i.e. are doing calculations where the exact number of days has to be totally correct, my suggestion is to always
TRUNC all dates so that your dates will be all looked at as if they were at midnight that day. You will then always receive a result that is a whole number
NOTE: I am going to discuss this in a later post, but be careful when you use the TRUNC function on dates in your
function.
If you i.e. have a WHERE statement similar to this .
1 WHERE TRUNC(o.order_date) BETWEEN SYSDATE - 30 AND SYSDATE
chances are very high that you will get an explain plan of your query showing a full tablescan on your order table..
Just mentioning it.
In this example we used the built-in Oracle MONTHS_BETWEEN function, which is pretty convenient to have some
times.
Using plain math to get the number of months between two dates
This is not a method I would recommend, but if you want to calculate months between two days, and you are using
Biblical months..being 30 days per monthit would work fine.
1 SQL> SELECT TRUNC((TO_DATE('20120325', 'YYYYMMDD') - TO_DATE('20120101', 'YYYYMMDD')) / 30) num_months
2
2
,(TO_DATE('20120325', 'YYYYMMDD') - TO_DATE('20120101', 'YYYYMMDD')) diff_in_days
3
3 FROM
dual
4
4 /
5
6 NUM_MONTHS DIFF_IN_DAYS
7 ---------- -----------8
2
84
26/10/2012 9:49 AM
3 of 7
7
7 FROM
8
8 /
9
10 START_DATE
11 ----------12 1/1/2012
dual
Most Popular
END_DATE
DIFF_IN_DAYS START_MONTH END_MONTH DIFF_IN_MONTHS
----------- ------------ ----------- ---------- -------------3/25/2012
84
1
3
2
(http://oracletuts.net/tutorials/three-ways-totranspose-rows-into-columns-in-oracle-sql/)
As you can see, the last field shows the result in the format +02-00. I am NOT going to go into the detailed usage of
this data type, but just know that you can set i.e. the precision of the number of digits you would want to uselike if
you only want to show the months, and not yearyou can specify (i.e.) that you want to show the months as three
digits. The default is 2.
If you want to extract the year and month values above, you can i.e. do like this:
1 SQL> SELECT y.start_date
2
2
,y.end_date
3
3
,y.diff_in_days
4
4
,y.diff_in_interval
5
5
,EXTRACT(YEAR FROM diff_in_interval) num_years
6
6
,EXTRACT(MONTH FROM diff_in_interval) num_months
7
7 FROM
(
8
8
SELECT x.start_date
9
9
,x.end_date
10
10
,(x.end_date - x.start_date) diff_in_days
11
11
,(x.end_date - x.start_date) YEAR TO MONTH diff_in_interval
12
12
FROM
(
SELECT TO_DATE('20100101', 'YYYYMMDD') start_date
13
13
14
14
,TO_DATE('20120515', 'YYYYMMDD') end_date
15
15
FROM
dual
16
16
) x
17
17
) y
18
18 /
(http://oracletuts.net/tutorials/how-to-convertunix-timestamp-to-date-format-in-plsql/)
How To Convert Unix Timestamp To Date
Format In PLSQL (http://oracletuts.net
/tutorials/how-to-convert-unix-timestampto-date-format-in-plsql/)
Read More (http://oracletuts.net/tutorials
/how-to-convert-unix-timestamp-to-date-formatin-plsql/) | 2 Comments (http://oracletuts.net
/tutorials/how-to-convert-unix-timestamp-to-dateformat-in-plsql/#comments) | 1838 Views
(http://oracletuts.net/tutorials/how-to-convertunix-timestamp-to-date-format-in-plsql/)
(http://oracletuts.net/articles/oracle-decodeand-case-what-is-the-difference/)
Oracle DECODE and CASE: What is the
difference (http://oracletuts.net/articles
/oracle-decode-and-case-what-isthe-difference/)
Read More (http://oracletuts.net/articles/oracledecode-and-case-what-is-the-difference/) | 1
Comment (http://oracletuts.net/articles/oracledecode-and-case-what-is-the-difference
/#comments) | 1631 Views (http://oracletuts.net
/articles/oracle-decode-and-case-what-isthe-difference/)
26/10/2012 9:49 AM
4 of 7
19
20 START_DATE END_DATE
DIFF_IN_DAYS DIFF_IN_INTERVAL NUM_YEARS NUM_MONTHS
21 ----------- ----------- ------------ ---------------- ---------- ---------22 1/1/2010
5/15/2012
865 +02-04
2
4
Blogging can be a lot of spare time work. Please help keep this site
going.
I have to admit that I have actually never used the INTERVAL data types, and to be honest there is a logic with this
data type that I am not sure if I totally understandor have thought through yet
If you look at the sample above, the end date is TO_DATE(20120515, YYYYMMDD). What if we change the end
date to i.e. TO_DATE(20120516, YYYYMMDD)one day later?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
You will then see that it suddenly shows an extra month. I am not sure exactly how the logic is meant to work, but it
looks like there is a difference in the middle of the month. If you have any explanation of this, your contribution is
welcomed.
In conclusion
As you have seen, using Oracle date arithmetic, there are many ways to get your result. In this tutorial I have shown
some of them, and if you have any additions to the examples..: Sharing is good.
I hope you enjoyed this tutorial, and that it helped you in your work.
26/10/2012 9:49 AM
5 of 7
Latest SQL
Latest PL/SQL
(http://oracletuts.net/tutorials
/introduction-to-aggregate-functions-in-oracle/)
(http://oracletuts.net/tutorials/how-totokenize-or-parse-a-string-in-plsql/)
Introduction To Aggregate
Functions In Oracle
(http://oracletuts.net/tutorials
/introduction-to-aggregatefunctions-in-oracle/)
Read More (http://oracletuts.net
/tutorials/introduction-to-aggregatefunctions-in-oracle/) | 1 Comment
(http://oracletuts.net/tutorials
/introduction-to-aggregate-functionsin-oracle/#comments) | 479 Views
(http://oracletuts.net/tutorials
/introduction-to-aggregate-functionsin-oracle/)
(http://oracletuts.net/tutorials/quicktip-slicing-a-long-plsql-string-into-smaller-pieces/)
(http://oracletuts.net/tutorials/how-towork-with-downline-hierarchies-in-oracle/)
How To Work With Downline
Hierarchies In Oracle
(http://oracletuts.net/tutorials
/how-to-work-with-downlinehierarchies-in-oracle/)
Read More (http://oracletuts.net
/tutorials/how-to-work-with-downlinehierarchies-in-oracle/) | No Comments
(http://oracletuts.net/tutorials/how-towork-with-downline-hierarchiesin-oracle/#comments) | 579 Views
(http://oracletuts.net/tutorials/how-towork-with-downline-hierarchiesin-oracle/)
(http://oracletuts.net/tutorials/how-toget-random-dates-in-oracle-plsql/)
(http://oracletuts.net/tutorials/how-torank-records-in-oracle-sql/)
How To Rank Records in Oracle
SQL (http://oracletuts.net/tutorials
/how-to-rank-records-in-oracle-sql/)
Read More (http://oracletuts.net
/tutorials/how-to-rank-recordsin-oracle-sql/) | 1 Comment
(http://oracletuts.net/tutorials/how-torank-records-in-oracle-sql/#comments)
| 765 Views (http://oracletuts.net
/tutorials/how-to-rank-recordsin-oracle-sql/)
PREVIOUS
NEXT
(http://oracletuts.net
/tutorials/how-toget-random-datesin-oracle-plsql/)
(http://oracletuts.net
/tutorials/three-ways-totranspose-rows-intocolumns-in-oracle-sql/)
Author
TJ Abrahamsen (http://oracletuts.net
/author/teab/)
TJ has worked in the IT business for 20+ years, whereof the last 14+ years
with Oracle. He has obtained an expertise in Oracle SQL and PL/SQL, and
(http://oracletuts.net/)
has a desire to share some of his love for Oracle development.
22
(http://oracletuts.net
/author/teab/)
article(s) published.
Visit my Website
(http://oracletuts.net/)
Follow me on Twitter
26/10/2012 9:49 AM
6 of 7
(http://twitter.com/oracletuts)
Say Something
COMMENT RULES:
It is fine to be critical, but if you you are rude, we will delete your comments. Please do not put your URL in the
comment text unless it is relevant to the post and please use your PERSONAL name, blogger name or initials
and not your business name, as the latter comes off like spam.
Have fun and thanks for adding to the conversation!
TJ Abrahamsen (http://oracletuts.net)
February 15th, 2012 (http://oracletuts.net/tutorials/how-to-calculate-difference-between-datesin-oracle-sql/#comment-30)
Hello Singh Thank you for your input. This is basically what is used in the three first examples.
~ TJ
Leave a Reply
Your Name
Your Email
Your Website
XHTML: You can use these tags: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
26/10/2012 9:49 AM
7 of 7
All content is copyright 2011 oracletuts.net - owned and operated by Exillum (http://exillum.com), United Stated.
Oracle*Tuts and it's sub-domains are not affiliated or endorsed by the Oracle Corporation. The oracle*tuts Learning Network's site(s) are not officially sponsored, or approved by the Oracle
Corporation.
The products and companies mentioned on this website may be the trademarks of their respective owners.
26/10/2012 9:49 AM