Obtain the time difference (in seconds) from two timestamps)

Source: Internet
Author: User
WhenyousubtracttwovariablesoftypeTIMESTAMP, runtime. IfthedatabaseisrunningonWindows, systimestampwillgenerallyh

When you subtract two variables of type TIMESTAMP, you get an interval day to second which has des a number of milliseconds and/or microseconds depending on the platform. if the database is running on Windows, systimestamp will generally h

When you subtract two variables of type TIMESTAMP, you get an interval day to second which has des a number of milliseconds and/or microseconds depending on the platform. if the database is running on Windows, systimestamp will generally have milliseconds. if the database is running on Unix, systimestamp will generally have microseconds.

1 select systimestamp-to_timestamp ('2017-07-23 ', 'yyyy-mm-dd ')
2 * from dual
SQL>/

SYSTIMESTAMP-TO_TIMESTAMP ('2017-07-23 ', 'yyyy-MM-DD ')
---------------------------------------------------------------------------
+ 000000000 14:51:04. 339000000
You can use the EXTRACT function to extract the inpidual elements of an INTERVAL DAY TO SECOND

SQL> ed
Wrote file afiedt. buf

Select extract (day from diff) days,
Extract (hour from diff) hours,
Extract (minute from diff) minutes,
Extract (second from diff) seconds
From (select effecimestamp-to_timestamp ('2017-07-23 ', 'yyyy-mm-dd') diff
From dual)

SQL>/

DAYS HOURS MINUTES SECONDS
----------------------------------------
0 14 55 37.936
You can then convert each of those components into milliseconds and add them up

SQL> ed
Wrote file afiedt. buf

1 select extract (day from diff) * 24*60*60*1000 +
2 extract (hour from diff) * 60*60*1000 +
3 extract (minute from diff) * 60*1000 +
4 round (extract (second from diff) * 1000) total_milliseconds
5 from (select systimestamp-to_timestamp ('2017-07-23 ', 'yyyy-mm-dd') diff
6 * from dual)
SQL>/

TOTAL_MILLISECONDS
------------------
53831842
Normally, however, it is more useful to have either the interval day to second representation or to have separate columns for hours, minutes, seconds, etc. rather than computing the total number of milliseconds between two TIMESTAMP values.

Subtraction between timestamps returns an INTERVAL datatype. you can use the EXTRACT function to return varous parts of an interval eg select extract (hour from (timestamp '2017-12-31 14:00:00 '-timestamp '2017-12-31 12:15:00 ')) hr from dual; Note: That only shows the HOUR part, so if the difference is 1 day and 1 hour, this will show 1 not 25. -Gary Myers Jul 8 '09

Another answer:

SQL> @ id8
SQL> drop table holder;

Table dropped.

SQL> create table holder (
2 beg_date timestamp,
3 end_date timestamp)
4/

Table created.

SQL> INSERT INTO HOLDER VALUES (to_timestamp ('2017-07-16: 19: 00: 8080', 'yyyy-MM-DD: HH24: MI: SS. ff '),
2 to_timestamp ('2017-08-17:20:00 ', 'yyyy-MM-DD: HH24: MI '));

1 row created.

SQL> COMMIT;

Commit complete.

SQL>

Select extract (day from (END_DATE-BEG_DATE) * 24*60*60 +
EXTRACT (hour from (END_DATE-BEG_DATE) * 60*60 +
EXTRACT (minute from (END_DATE-BEG_DATE) * 60 +
EXTRACT (second from (END_DATE-BEG_DATE) DELTA
FROM holder


DELTA
----------
2768398.5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.