how to calculate difference between dates in oracle

7
How To Calculate Difference Between Dates in Oracle SQL Posted February 26th, 2012 by TJ Abrahamsen ( http://oracletuts.net/author/teab/ ) & filed under Tutorials ( http://ora- cletuts.net/category/tutorials/ ) . ( http://oracletuts.net/assets/date_diff.jpg )If you have worked with SQL for a while – you have most likely had a time where you wanted to show in a query the difference be- tween two dates. It can be for example to calculate how long a person has been hired in your company, how old a person is today, etc. In this tutorial we are going to look at some ways to do this in Oracle SQL. 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 func- tions, 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 af- terwards. Anyway…let’s get on with it. The use of TRUNC with dates 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. The time part of a TRUNC-ed date 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 mid- night (“12:00:00 AM) that day. 1 SQL> SELECT TO_CHAR(SYSDATE, 'MM/DD/YYYY HH:MI:SS AM') right_now_trunced 2 2 ,TRUNC(SYSDATE) today 3 3 ,TO_CHAR(TRUNC(SYSDATE), 'MM/DD/YYYY HH:MI:SS AM') right_now_trunced 4 4 FROM dual 5 5 / 6 7 RIGHT_NOW_TRUNCED TODAY RIGHT_NOW_TRUNCED 8 ---------------------- ----------- ---------------------- 9 01/31/2012 06:24:57 PM 1/31/2012 01/31/2012 12:00:00 AM 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 RSS LinkedIn Google Facebook Twitter Youtube Advertise Here (http://oracletuts.net /advertise Grab Your Free Newsletter! Get the best of our site delivered directly to your inbox. Enter email address Subscribe To Our Newsletter! Advertise Here (http://oracletuts.net /advertise /?instructions=highlightzone Social Media Recommend on Google Follow @YOU ( https://twitter.com/oracletuts ) Subscribe To Our Newsletter Recent Comments ALI { I WANT TO SOLVE MY SOME PROBLEMS IN ORACLE BY EASY STEPS } – Oct 10, 10:13 AM ( http://oracletuts.net/tutorials/introduction-to-aggregate- functions-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-in- plsql/#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- Subscribe ( http://feeds.feedburner.com /oracletutsnetwork ) Get Linked ( http://www.linkedin.com /in/tabrahamsen ) Follow Us ( https://plus.google.com /u/0/110990718337941106533 ) Follow Us ( http://www.facebook.com /oracletuts ) Follow Us ( http://twitter.com /oracletuts ) Follow Us ( https://youtube.com /user/oracletuts ) How To Calculate Difference Between Dates in Oracle SQL | ORACLETUTS http://oracletuts.net/tutorials/how-to-calculate-difference-between-dates-i... 1 of 7 26/10/2012 9:49 AM

Upload: rama-chandra-iv

Post on 17-Jul-2016

18 views

Category:

Documents


0 download

DESCRIPTION

Date Diff

TRANSCRIPT

Page 1: How to Calculate Difference Between Dates in Oracle

How To Calculate Difference Between Dates in Oracle SQL

Posted February 26th, 2012 by TJ Abrahamsen (http://oracletuts.net/author/teab/) & filed under Tutorials (http://ora-

cletuts.net/category/tutorials/).

(http://oracletuts.net/assets/date_diff.jpg)If you have worked with SQL for a while – you

have most likely had a time where you wanted to show in a query the difference be-tween two dates .

It can be for example to calculate how long a person has been hired in your company,

how old a person is today, etc.

In this tutorial we are going to look at some ways to do this in Oracle SQL .

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 func-tions , 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 af-

terwards.

Anyway…let’s get on with it.

The use of TRUNC with dates

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.

The time part of a TRUNC-ed date

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 mid-

night (“12:00:00 AM) that day.

1 SQL> SELECT TO_CHAR(SYSDATE, 'MM/DD/YYYY HH:MI:SS AM') right_now_trunced2 2 ,TRUNC(SYSDATE) today3 3 ,TO_CHAR(TRUNC(SYSDATE), 'MM/DD/YYYY HH:MI:SS AM') right_now_trunced4 4 FROM dual5 5 /6 7 RIGHT_NOW_TRUNCED TODAY RIGHT_NOW_TRUNCED8 ---------------------- ----------- ----------------------9 01/31/2012 06:24:57 PM 1/31/2012 01/31/2012 12:00:00 AM

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_diff3 3 FROM dual4 4 /5 6 DAY_DIFF

RSS LinkedIn Google

Facebook Twitter Youtube

Advertise

Here (http://oracletuts.net

/advertise

Grab Your Free Newsletter!

Get the best of our site delivereddirectly to your inbox.

Enter email address

Subscribe To Our Newsletter!

Advertise

Here (http://oracletuts.net

/advertise

/?instructions=highlightzone837

Social Media

Recommend on Google

Follow @YOU (https://twitter.com/oracletuts)

Subscribe To Our Newsletter

Recent Comments

ALI { I WANT TO SOLVE MY SOME PROBLEMS IN

ORACLE BY EASY STEPS } – Oct 10, 10:13 AM

(http://oracletuts.net/tutorials/introduction-to-aggregate-functions-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-in-

plsql/#comment-99)

Bari { Good work, Thanks for explaining transpose

query,have been searching for this one for a while. } – Sep11, 3:38 AM (http://oracletuts.net/tutorials/three-ways-to-

Subscribe

(http://feeds.feedburner.com

/oracletutsnetwork)

Get Linked

(http://www.linkedin.com

/in/tabrahamsen)

Follow Us

(https://plus.google.com

/u/0/110990718337941106533)

Follow Us

(http://www.facebook.com

/oracletuts)

Follow Us

(http://twitter.com

/oracletuts)

Follow Us

(https://youtube.com

/user/oracletuts)

How To Calculate Difference Between Dates in Oracle SQL | ORACLETUTS http://oracletuts.net/tutorials/how-to-calculate-difference-between-dates-i...

1 of 7 26/10/2012 9:49 AM

Page 2: How to Calculate Difference Between Dates in Oracle

If you i.e. are doing calculations where the exact number of days has to be totally correct, my suggestion is to alwaysTRUNC all dates so that your dates will be all looked at as if they were at midnight that day. You will then always re-

ceive 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 ….

… 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….

Date difference examplesAlrighty then…let’s look of some different examples of date difference functionality. Please note that I will be using

dates in these samples that do not have a time part set.

Example # 1: How to get the number of days between two dates

The simplest form of showing this is using functionality we have already shown:

Ok, that was a bit too simple..

Example # 2: How to get number of months between two dates

Using the MONTHS_BETWEEN function

In this example we used the built-in Oracle MONTHS_BETWEEN function, which is pretty convenient to have sometimes.

Using plain math to get the number of months betwee n 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 month…it would work fine.

Using the EXTRACT function

In this example we are going to use the EXTRACT function to find the the month number, and then do a subtraction.

7 ----------8 59 SQL> SELECT TO_DATE('20120125 10:00:00 AM', 'YYYYMMDD HH:MI:SS AM')

10 2 - TO_DATE('20120120 11:00:00 AM', 'YYYYMMDD HH:MI:SS AM') day_diff11 3 FROM dual12 4 /13 14 DAY_DIFF15 ----------16 4.9583333317 SQL> SELECT TO_DATE('20120125 11:00:00 AM', 'YYYYMMDD HH:MI:SS AM')18 2 - TO_DATE('20120120 10:00:00 AM', 'YYYYMMDD HH:MI:SS AM') day_diff19 3 FROM dual20 4 /21 22 DAY_DIFF23 ----------24 5.04166666

1 WHERE TRUNC(o.order_date) BETWEEN SYSDATE - 30 AND SYSDATE

1 SQL> SELECT TO_DATE('20120125', 'YYYYMMDD') - TO_DATE('20120120', 'YYYYMMDD') day_diff2 2 FROM dual3 3 /4 5 DAY_DIFF6 ----------7 5

1 SQL> SELECT MONTHS_BETWEEN(TO_DATE('20120325', 'YYYYMMDD'), TO_DATE('20120101', 'YYYYMMDD'2 2 ,(TO_DATE('20120325', 'YYYYMMDD') - TO_DATE('20120101', 'YYYYMMDD')) diff_in_days3 3 FROM dual4 4 /5 6 NUM_MONTHS DIFF_IN_DAYS7 ---------- ------------8 2.77419354 84

1 SQL> SELECT TRUNC((TO_DATE('20120325', 'YYYYMMDD') - TO_DATE('20120101', 'YYYYMMDD')) / 30) num_months2 2 ,(TO_DATE('20120325', 'YYYYMMDD') - TO_DATE('20120101', 'YYYYMMDD')) diff_in_days3 3 FROM dual4 4 /5 6 NUM_MONTHS DIFF_IN_DAYS7 ---------- ------------8 2 84

1 SQL> SELECT TO_DATE('20120101', 'YYYYMMDD') start_date2 2 ,TO_DATE('20120325', 'YYYYMMDD') end_date3 3 ,(TO_DATE('20120325', 'YYYYMMDD') - TO_DATE('20120101', 'YYYYMMDD')) diff_in_days4 4 ,EXTRACT(MONTH FROM TO_DATE('20120101', 'YYYYMMDD')) start_month5 5 ,EXTRACT(MONTH FROM TO_DATE('20120325', 'YYYYMMDD')) end_month6 6 ,(EXTRACT(MONTH FROM TO_DATE('20120325', 'YYYYMMDD')) - EXTRACT(MONTH FROM

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-transpose-

rows-into-columns-in-oracle-sql/#comment-90)

»» (http://oracletuts.net/tutorials/how-to-calculate-difference-

between-dates-in-oracle-sql/?bwprc_paged=2)

How To Calculate Difference Between Dates in Oracle SQL | ORACLETUTS http://oracletuts.net/tutorials/how-to-calculate-difference-between-dates-i...

2 of 7 26/10/2012 9:49 AM

Page 3: How to Calculate Difference Between Dates in Oracle

But, what if there are different years on our two dates…?

Example # 3: How to get the number of years and months between two dates

One way to do this is actually to use the MONTHS_BETWEEN function like this:

But, if we want to show the number of years as well, we can do something like this:

Another way to do show the number of years and months could be something like this:

Example # 4: How to get number of years and months between two dates using INTERVAL datatypes

The INTERVAL data types were introduced in Oracle 9i. There are two of them, but in this example we are going to

use the one declared as “INTERVAL YEAR TO MONTH “.

An example of usage of this in an Oracle SQL statement can be the following:

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 use…like if

you only want to show the months, and not year…you can specify (i.e.) that you want to show the months as threedigits. The default is 2.

If you want to extract the year and month values above, you can i.e. do like this:

7 7 FROM dual8 8 /9

10 START_DATE END_DATE DIFF_IN_DAYS START_MONTH END_MONTH DIFF_IN_MONTHS11 ----------- ----------- ------------ ----------- ---------- --------------12 1/1/2012 3/25/2012 84 1 3 2

1 SQL> SELECT TRUNC(MONTHS_BETWEEN(TO_DATE('20120325', 'YYYYMMDD'), TO_DATE('20100101', 2 2 ,(TO_DATE('20120325', 'YYYYMMDD') - TO_DATE('20100101', 'YYYYMMDD')) diff_in_days3 3 FROM dual4 4 /5 6 TRUNC(MONTHS_BETWEEN(TO_DATE(' DIFF_IN_DAYS7 ------------------------------ ------------8 26 814

1 SQL> SELECT (TO_DATE('20120525', 'YYYYMMDD') - TO_DATE('20100101', 'YYYYMMDD')) diff_in_days2 2 ,TRUNC(MONTHS_BETWEEN(TO_DATE('20120525', 'YYYYMMDD'), TO_DATE('20100101', 3 3 ,TRUNC(TRUNC(MONTHS_BETWEEN(TO_DATE('20120525', 'YYYYMMDD'), TO_DATE('20100101'4 4 ,MOD(TRUNC(MONTHS_BETWEEN(TO_DATE('20120525', 'YYYYMMDD'), TO_DATE('20100101'5 5 FROM dual6 6 /7 8 DIFF_IN_DAYS TRUNC(MONTHS_BETWEEN(TO_DATE(' NUM_YEARS NUM_MONTHS9 ------------ ------------------------------ ---------- ----------

10 875 28 2 4

1 SQL> SELECT TO_DATE('20100101', 'YYYYMMDD') start_date2 2 ,TO_DATE('20120525', 'YYYYMMDD') end_date3 3 ,(TO_DATE('20120525', 'YYYYMMDD') - TO_DATE('20100101', 'YYYYMMDD')) diff_in_days4 4 ,EXTRACT(YEAR FROM TO_DATE('20100101', 'YYYYMMDD')) start_month5 5 ,EXTRACT(YEAR FROM TO_DATE('20120525', 'YYYYMMDD')) end_month6 6 ,EXTRACT(MONTH FROM TO_DATE('20100101', 'YYYYMMDD')) start_month7 7 ,EXTRACT(MONTH FROM TO_DATE('20120525', 'YYYYMMDD')) end_month8 8 ,(EXTRACT(YEAR FROM TO_DATE('20120525', 'YYYYMMDD')) - EXTRACT(YEAR FROM TO_DATE(9 9 ,(EXTRACT(MONTH FROM TO_DATE('20120525', 'YYYYMMDD')) - EXTRACT(MONTH FROM

10 10 FROM dual11 11 /12 13 START_DATE END_DATE DIFF_IN_DAYS START_MONTH END_MONTH START_MONTH END_MONTH DIFF_IN_YEARS DIFF_IN_14 ----------- ----------- ------------ ----------- ---------- ----------- ---------- ------------- --------15 1/1/2010 5/25/2012 875 2010 2012 1 5 2

1 SQL> SELECT x.start_date2 2 ,x.end_date3 3 ,(x.end_date - x.start_date) diff_in_days4 4 ,(x.end_date - x.start_date) YEAR TO MONTH diff_in_months5 5 FROM (6 6 SELECT TO_DATE('20100101', 'YYYYMMDD') start_date7 7 ,TO_DATE('20120515', 'YYYYMMDD') end_date8 8 FROM dual9 9 ) x

10 10 /11 12 START_DATE END_DATE DIFF_IN_DAYS DIFF_IN_MONTHS13 ----------- ----------- ------------ ---------------14 1/1/2010 5/15/2012 865 +02-04

1 SQL> SELECT y.start_date2 2 ,y.end_date3 3 ,y.diff_in_days4 4 ,y.diff_in_interval5 5 ,EXTRACT(YEAR FROM diff_in_interval) num_years6 6 ,EXTRACT(MONTH FROM diff_in_interval) num_months7 7 FROM (8 8 SELECT x.start_date9 9 ,x.end_date

10 10 ,(x.end_date - x.start_date) diff_in_days11 11 ,(x.end_date - x.start_date) YEAR TO MONTH diff_in_interval12 12 FROM (13 13 SELECT TO_DATE('20100101', 'YYYYMMDD') start_date14 14 ,TO_DATE('20120515', 'YYYYMMDD') end_date15 15 FROM dual16 16 ) x17 17 ) y18 18 /

|

|

|

|

|

|

|

|

|

|

Most Popular

Help us keep on bloggin’

(http://oracletuts.net/tutorials/three-ways-to-

transpose-rows-into-columns-in-oracle-sql/)

Three Ways To Transpose Rows IntoColumns in Oracle SQL (http://oracletuts.net/tutorials/three-ways-to-transpose-rows-into-columns-in-oracle-sql/)Read More (http://oracletuts.net/tutorials/three-

ways-to-transpose-rows-into-columns-

in-oracle-sql/) 5 Comments

(http://oracletuts.net/tutorials/three-ways-to-transpose-rows-into-columns-in-oracle-

sql/#comments) 2712 Views

(http://oracletuts.net/tutorials/three-ways-to-

transpose-rows-into-columns-in-oracle-sql/)

(http://oracletuts.net/tutorials/how-to-calculate-

difference-between-dates-in-oracle-sql/)

How To Calculate Difference Between Datesin Oracle SQL (http://oracletuts.net/tutorials/how-to-calculate-difference-between-dates-in-oracle-sql/)Read More (http://oracletuts.net/tutorials

/how-to-calculate-difference-between-dates-in-oracle-sql/) 2 Comments

(http://oracletuts.net/tutorials/how-to-calculate-

difference-between-dates-in-oracle-

sql/#comments) 2270 Views

(http://oracletuts.net/tutorials/how-to-calculate-

difference-between-dates-in-oracle-sql/)

(http://oracletuts.net/tutorials/how-to-use-oracle-analytic-functions-in-oracle-sql/)

How To Use Oracle Analytic Functions inOracle SQL (http://oracletuts.net/tutorials/how-to-use-oracle-analytic-functions-in-oracle-sql/)Read More (http://oracletuts.net/tutorials

/how-to-use-oracle-analytic-functions-

in-oracle-sql/) 7 Comments

(http://oracletuts.net/tutorials/how-to-use-oracle-

analytic-functions-in-oracle-sql/#comments)

2201 Views (http://oracletuts.net/tutorials

/how-to-use-oracle-analytic-functions-

in-oracle-sql/)

(http://oracletuts.net/tutorials/how-to-convert-

unix-timestamp-to-date-format-in-plsql/)

How To Convert Unix Timestamp To DateFormat In PLSQL (http://oracletuts.net/tutorials/how-to-convert-unix-timestamp-to-date-format-in-plsql/)Read More (http://oracletuts.net/tutorials

/how-to-convert-unix-timestamp-to-date-format-

in-plsql/) 2 Comments (http://oracletuts.net

/tutorials/how-to-convert-unix-timestamp-to-date-

format-in-plsql/#comments) 1838 Views(http://oracletuts.net/tutorials/how-to-convert-

unix-timestamp-to-date-format-in-plsql/)

(http://oracletuts.net/articles/oracle-decode-

and-case-what-is-the-difference/)

Oracle DECODE and CASE: What is thedifference (http://oracletuts.net/articles/oracle-decode-and-case-what-is-the-difference/)Read More (http://oracletuts.net/articles/oracle-

decode-and-case-what-is-the-difference/) 1

Comment (http://oracletuts.net/articles/oracle-decode-and-case-what-is-the-difference

/#comments) 1631 Views (http://oracletuts.net

/articles/oracle-decode-and-case-what-is-

the-difference/)

How To Calculate Difference Between Dates in Oracle SQL | ORACLETUTS http://oracletuts.net/tutorials/how-to-calculate-difference-between-dates-i...

3 of 7 26/10/2012 9:49 AM

Page 4: How to Calculate Difference Between Dates in Oracle

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 understand…or 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?

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 iswelcomed.

In conclusionAs 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.

Tags: date (http://oracletuts.net/tag/date/), function (http://oracletuts.net/tag/function/), sql (http://oracletuts.net

/tag/sql/)

19 20 START_DATE END_DATE DIFF_IN_DAYS DIFF_IN_INTERVAL NUM_YEARS NUM_MONTHS21 ----------- ----------- ------------ ---------------- ---------- ----------22 1/1/2010 5/15/2012 865 +02-04 2 4

1 SQL> SELECT y.start_date2 2 ,y.end_date3 3 ,y.diff_in_days4 4 ,y.diff_in_interval5 5 ,EXTRACT(YEAR FROM diff_in_interval) num_years6 6 ,EXTRACT(MONTH FROM diff_in_interval) num_months7 7 FROM (8 8 SELECT x.start_date9 9 ,x.end_date

10 10 ,(x.end_date - x.start_date) diff_in_days11 11 ,(x.end_date - x.start_date) YEAR TO MONTH diff_in_interval12 12 FROM (13 13 SELECT TO_DATE('20100101', 'YYYYMMDD') start_date14 14 ,TO_DATE('20120516', 'YYYYMMDD') end_date15 15 FROM dual16 16 ) x17 17 ) y18 18 /19 20 START_DATE END_DATE DIFF_IN_DAYS DIFF_IN_INTERVAL NUM_YEARS NUM_MONTHS21 ----------- ----------- ------------ ------------------ ---------- ----------22 1/1/2010 5/16/2012 866 +02-05 2 5

Blogging can be a lot of spare time work. Please help keep this site

going.

How To Calculate Difference Between Dates in Oracle SQL | ORACLETUTS http://oracletuts.net/tutorials/how-to-calculate-difference-between-dates-i...

4 of 7 26/10/2012 9:49 AM

Page 5: How to Calculate Difference Between Dates in Oracle

|

|

|

|

|

|

Latest SQL

|

|

|

|

|

|

Latest PL/SQL

« PREVIOUS

(http://oracletuts.net

/tutorials/how-to-

get-random-dates-

in-oracle-plsql/)

NEXT »

(http://oracletuts.net

/tutorials/three-ways-to-

transpose-rows-into-

columns-in-oracle-sql/)

(http://oracletuts.net/)

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

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

Author

(http://oracletuts.net/tutorials

/introduction-to-aggregate-functions-in-oracle/)

Introduction To AggregateFunctions In Oracle(http://oracletuts.net/tutorials/introduction-to-aggregate-functions-in-oracle/)Read More (http://oracletuts.net

/tutorials/introduction-to-aggregate-

functions-in-oracle/) 1 Comment

(http://oracletuts.net/tutorials/introduction-to-aggregate-functions-

in-oracle/#comments) 479 Views

(http://oracletuts.net/tutorials

/introduction-to-aggregate-functions-

in-oracle/)

(http://oracletuts.net/tutorials/how-to-

work-with-downline-hierarchies-in-oracle/)

How To Work With DownlineHierarchies In Oracle(http://oracletuts.net/tutorials/how-to-work-with-downline-hierarchies-in-oracle/)Read More (http://oracletuts.net

/tutorials/how-to-work-with-downline-

hierarchies-in-oracle/) No Comments(http://oracletuts.net/tutorials/how-to-

work-with-downline-hierarchies-

in-oracle/#comments) 579 Views

(http://oracletuts.net/tutorials/how-to-

work-with-downline-hierarchies-

in-oracle/)

(http://oracletuts.net/tutorials/how-to-rank-records-in-oracle-sql/)

How To Rank Records in OracleSQL (http://oracletuts.net/tutorials/how-to-rank-records-in-oracle-sql/)Read More (http://oracletuts.net

/tutorials/how-to-rank-records-

in-oracle-sql/) 1 Comment

(http://oracletuts.net/tutorials/how-to-

rank-records-in-oracle-sql/#comments)765 Views (http://oracletuts.net

/tutorials/how-to-rank-records-

in-oracle-sql/)

(http://oracletuts.net/tutorials/how-to-

tokenize-or-parse-a-string-in-plsql/)

How To Tokenize Or Parse A StringIn PLSQL (http://oracletuts.net/tutorials/how-to-tokenize-or-parse-a-string-in-plsql/)Read More (http://oracletuts.net/tutorials/how-to-tokenize-or-parse-

a-string-in-plsql/) No Comments

(http://oracletuts.net/tutorials/how-to-

tokenize-or-parse-a-string-in-plsql

/#comments) 1178 Views

(http://oracletuts.net/tutorials/how-to-

tokenize-or-parse-a-string-in-plsql/)

(http://oracletuts.net/tutorials/quick-

tip-slicing-a-long-plsql-string-into-smaller-pieces/)

Quick Tip: Slicing A Long PLSQLString Into Smaller Pieces(http://oracletuts.net/tutorials/quick-tip-slicing-a-long-plsql-string-into-smaller-pieces/)Read More (http://oracletuts.net/tutorials/quick-tip-slicing-a-long-plsql-

string-into-smaller-pieces/) 1

Comment (http://oracletuts.net/tutorials

/quick-tip-slicing-a-long-plsql-string-

into-smaller-pieces/#comments) 812

Views (http://oracletuts.net/tutorials

/quick-tip-slicing-a-long-plsql-string-

into-smaller-pieces/)

(http://oracletuts.net/tutorials/how-to-

get-random-dates-in-oracle-plsql/)

How To Get Random Dates InOracle PLSQL (http://oracletuts.net/tutorials/how-to-get-random-dates-in-oracle-plsql/)Read More (http://oracletuts.net

/tutorials/how-to-get-random-dates-

in-oracle-plsql/) 1 Comment

(http://oracletuts.net/tutorials/how-to-

get-random-dates-in-oracle-

plsql/#comments) 1010 Views(http://oracletuts.net/tutorials/how-to-

get-random-dates-in-oracle-plsql/)

How To Calculate Difference Between Dates in Oracle SQL | ORACLETUTS http://oracletuts.net/tutorials/how-to-calculate-difference-between-dates-i...

5 of 7 26/10/2012 9:49 AM

Page 6: How to Calculate Difference Between Dates in Oracle

(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!

2 Responses to “How To Calculate Difference Between Dates in Oracle SQL”

Leave a Reply

Your Name

Your Email

Your Website

Your Comment Here...

CAPTCHA CodePlease enter the letters and numbers you see in

the picture above before you post the comment

Submit Comment

XHTML: You can use these tags: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <block-

quote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

February 15th, 2012 (http://oracletuts.net/tutorials/how-to-calculate-difference-between-dates-

in-oracle-sql/#comment-28)

Singh

Oracle support Mathematical Subtract ‘-’ operator on Data datatype. You may directly put in se-

lect clause following statement:

to_char (s.last_upd – s.created, ’999999D99′)

{Links moderated away from comment}

February 15th, 2012 (http://oracletuts.net/tutorials/how-to-calculate-difference-between-dates-

in-oracle-sql/#comment-30)

TJ Abrahamsen (http://oracletuts.net )

Hello Singh -

Thank you for your input. This is basically what is used in the three first examples.

~ TJ

How To Calculate Difference Between Dates in Oracle SQL | ORACLETUTS http://oracletuts.net/tutorials/how-to-calculate-difference-between-dates-i...

6 of 7 26/10/2012 9:49 AM

Page 7: How to Calculate Difference Between Dates in Oracle

About (http://oracletuts.net/about/) Copyright & Disclaimer (http://oracletuts.net/copyright-disclaimer/)

Contact Us (http://oracletuts.net/contact)

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.

How To Calculate Difference Between Dates in Oracle SQL | ORACLETUTS http://oracletuts.net/tutorials/how-to-calculate-difference-between-dates-i...

7 of 7 26/10/2012 9:49 AM