Solved

Oracle date time arithmetic: subtracting two dates to get minutes

Posted on 2012-03-27
4
765 Views
Last Modified: 2012-08-13
Hi there,

I need to subtract two datetime fields in Oracle to get the difference in minutes.
for example:
begin_lab_time = 2011.12.12 08:10:00
end_lab_time = 2011.12.12 08:31:00
expected result from subtracting these two fields: 21 minutes

How would I go about getting the expected result in Oracle?

I tried this but I get 20 minutes as the result and not 21 minutes. I need to get 21 minutes as the result:
trunc((86400*(end_lab_time-begin_lab_time))/60)-60*(trunc(((86400*(end_lab_time-begin_lab_time))/60)/60))
0
Comment
Question by:wdelaney05
  • 2
  • 2
4 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
Comment Utility
(end_lab_time - begin_lab_time) * 1440
0
 

Author Comment

by:wdelaney05
Comment Utility
ah. getting closer. I get 20.99999999999999999999999999999999999995 as the result, sdstuber. Is it best to use ROUND, then to get 21?
0
 
LVL 73

Expert Comment

by:sdstuber
Comment Utility
interesting.... I get different results based on whether I'm executing within pl/sql context or not.

SQL> SELECT (end_lab_time - begin_lab_time) * 1440
  2    FROM (SELECT TO_DATE('2011.12.12 08:10:00', 'yyyy.mm.dd hh24:mi:ss') begin_lab_time,
  3                 TO_DATE('2011.12.12 08:31:00', 'yyyy.mm.dd hh24:mi:ss') end_lab_time
  4            FROM DUAL);

(END_LAB_TIME-BEGIN_LAB_TIME)*1440
----------------------------------
                                21

SQL> DECLARE
  2      v_result         NUMBER;
  3      begin_lab_time   DATE := TO_DATE('2011.12.12 08:10:00', 'yyyy.mm.dd hh24:mi:ss');
  4      end_lab_time     DATE := TO_DATE('2011.12.12 08:31:00', 'yyyy.mm.dd hh24:mi:ss');
  5  BEGIN
  6      v_result  := (end_lab_time - begin_lab_time) * 1440;
  7      DBMS_OUTPUT.put_line(v_result);
  8
  9      SELECT (end_lab_time - begin_lab_time) * 1440 INTO v_result FROM DUAL;
 10
 11      DBMS_OUTPUT.put_line(v_result);
 12
 13      DBMS_OUTPUT.put_line((end_lab_time - begin_lab_time) * 1440);
 14  END;
 15  /
20.99999999999999999999999999999999999995
20.99999999999999999999999999999999999995
20.99999999999999999999999999999999999995

PL/SQL procedure successfully completed.

Open in new window



So, I guess...
yes, if you want to do your date math in pl/sql then you'll have to round it to get your results.
0
 

Author Comment

by:wdelaney05
Comment Utility
Re: So, I guess...
yes, if you want to do your date math in pl/sql then you'll have to round it to get your results.

ok, for my purposes, I will use ROUND; that works for my results.
Thanks very much for your help, it's much appreciated!
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

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

772 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

10 Experts available now in Live!

Get 1:1 Help Now