Solved

Oracle date time arithmetic: subtracting two dates to get minutes

Posted on 2012-03-27
4
791 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 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 37772326
(end_lab_time - begin_lab_time) * 1440
0
 

Author Comment

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

Expert Comment

by:sdstuber
ID: 37772485
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
ID: 37772507
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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 …
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 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.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

820 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