Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating difference between two timestamps in Oracle in milliseconds

How do I calculate the time difference in milliseconds between two timestamps in Oracle?

like image 347
sarsnake Avatar asked Jul 23 '12 18:07

sarsnake


People also ask

How can I find the difference between two timestamps in Oracle seconds?

To calculate the difference between the timestamps in Oracle, simply subtract the start timestamp from the end timestamp (here: arrival - departure ). The resulting column will be in INTERVAL DAY TO SECOND .

How do you find the difference in time between two timestamps?

Discussion: If you'd like to calculate the difference between the timestamps in seconds, multiply the decimal difference in days by the number of seconds in a day, which equals 24 * 60 * 60 = 86400 , or the product of the number of hours in a day, the number of minutes in an hour, and the number of seconds in a minute.

Does Oracle timestamp have milliseconds?

Oracle date data types do not support milliseconds but Oracle timestamps do.

How do I get the time difference between two timestamps in SQL?

To calculate the difference between the timestamps in MySQL, use the TIMESTAMPDIFF(unit, start, end) function. The unit argument can be MICROSECOND , SECOND , MINUTE , HOUR , DAY , WEEK , MONTH , QUARTER , or YEAR .

How to calculate the difference between the timestamps in Oracle?

To calculate the difference between the timestamps in Oracle, simply subtract the start timestamp from the end timestamp (here: arrival - departure). The resulting column will be in INTERVAL DAY TO SECOND. The first number you see is the number of whole days that passed from departure to arrival.

How to get the total milliseconds of a timestamp?

Expanding René's answer a bit, if you want the total milliseconds then you need to extract and combine all of the elements from the interval that's produced by subtracting one timestamp from another:

How to get the total number of milliseconds in an interval?

Show activity on this post. Expanding René's answer a bit, if you want the total milliseconds then you need to extract and combine all of the elements from the interval that's produced by subtracting one timestamp from another:

How do I calculate the difference between arrival and departure timestamps?

In the travel table, there are three columns: id, departure, and arrival. You'd like to calculate the difference between the arrival and the departure. To calculate the difference between the timestamps in Oracle, simply subtract the start timestamp from the end timestamp (here: arrival - departure ).


2 Answers

When you subtract two variables of type TIMESTAMP, you get an INTERVAL DAY TO SECOND which includes 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( '2012-07-23', 'yyyy-mm-dd' )   2*   from dual SQL> /  SYSTIMESTAMP-TO_TIMESTAMP('2012-07-23','YYYY-MM-DD') --------------------------------------------------------------------------- +000000000 14:51:04.339000000 

You can use the EXTRACT function to extract the individual elements of an INTERVAL DAY TO SECOND

SQL> ed Wrote file afiedt.buf    1  select extract( day from diff ) days,   2         extract( hour from diff ) hours,   3         extract( minute from diff ) minutes,   4         extract( second from diff ) seconds   5    from (select systimestamp - to_timestamp( '2012-07-23', 'yyyy-mm-dd' ) diff   6*           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( '2012-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.

like image 124
Justin Cave Avatar answered Oct 10 '22 12:10

Justin Cave


Here's a stored proc to do it:

CREATE OR REPLACE function timestamp_diff(a timestamp, b timestamp) return number is  begin   return extract (day    from (a-b))*24*60*60 +          extract (hour   from (a-b))*60*60+          extract (minute from (a-b))*60+          extract (second from (a-b)); end; / 

Up Vote if you also wanted to beat the crap out of the Oracle developer who negated to his job!

BECAUSE comparing timestamps for the first time should take everyone an hour or so...

like image 43
Brian McGinity Avatar answered Oct 10 '22 12:10

Brian McGinity