Thanks for the question, Partha.
Asked: January 11, 2002 - 12:21 pm UTC
Last updated: June 18, 2004 - 10:59 am UTC
Version: 8.1.6
Viewed 1000+ times
You Asked
Tom,
I have follwing dates and i want's to display difference in minutes,
when i use follwing query , i'm not getting why?
E_COMPLETION_TIME = '12/04/2001 11:32:00'
E_BEGIN_TIME = '12/04/2001 11:31:00'
select TRUNC(MOD(("LMF"."EVENT_DETAILS"."E_COMPLETION_TIME" - "LMF"."EVENT_DETAILS"."E_BEGIN_TIME")*1440,60)) Minutes,
from event_details;
Actually i should get one minute difference in the result, but i'm getting 0 why?
Can you please give me the explaination.
Thanks in advance.
and Tom said...
It is small errors introduced in the mixed mode arithmetic. It sees "ints", "reals", large scale, small scale numbers and rounding errors creep in.
If you use 24*60 (which I personally find more readable), the math works. If you through in a little extra math to force the constant to a real, it works:
ops$tkyte@ORA815.US.ORACLE.COM> select x, y from t;
X Y
------------------- -------------------
12/04/2001 11:32:00 12/04/2001 11:31:00
ops$tkyte@ORA815.US.ORACLE.COM> select
2 to_char( (x-y)*1440, '9.99999999999999999999999999999999999999999' )
3 from t;
TO_CHAR((X-Y)*1440,'9.9999999999999999999999
--------------------------------------------
.99999999999999999999999999999999999999940
ops$tkyte@ORA815.US.ORACLE.COM> select
2 to_char( round((x-y)*1440,5), '9.99999999999999999999999999999999999999999' )
3 from t;
TO_CHAR(ROUND((X-Y)*1440,5),'9.9999999999999
--------------------------------------------
1.00000000000000000000000000000000000000000
ops$tkyte@ORA815.US.ORACLE.COM> select
2 to_char( (x-y)*24*60, '9.99999999999999999999999999999999999999999' )
3 from t;
TO_CHAR((X-Y)*24*60,'9.999999999999999999999
--------------------------------------------
1.00000000000000000000000000000000000000000
ops$tkyte@ORA815.US.ORACLE.COM> select
2 to_char( (x-y)*2*1440/2, '9.99999999999999999999999999999999999999999' )
3 from t;
TO_CHAR((X-Y)*2*1440/2,'9.999999999999999999
--------------------------------------------
1.00000000000000000000000000000000000000000
See the (x-y)*1440 is just a little smaller than one (you normally won't see that)
Consider:
ops$tkyte@ORA815.US.ORACLE.COM> select TRUNC(MOD((x - y)*1440,60)) from t;
TRUNC(MOD((X-Y)*1440,60))
-------------------------
.000000000
ops$tkyte@ORA815.US.ORACLE.COM> select MOD((x - y)*2*1440/2,60) from t;
MOD((X-Y)*2*1440/2,60)
----------------------
1.000000000
ops$tkyte@ORA815.US.ORACLE.COM> select MOD(round((x - y)*1440,5),60) from t;
MOD(ROUND((X-Y)*1440,5),60)
---------------------------
1.000000000
ops$tkyte@ORA815.US.ORACLE.COM> select MOD((x - y)*24*60,60) from t;
MOD((X-Y)*24*60,60)
-------------------
1.000000000
rounding to 5 decimal places (biggest number of decimals in a day with seconds), can correct this as well.
Rating
(3 ratings)
Is this answer out of date? If it is, please let us know via a Comment