How do I calculate the time difference in milliseconds between two 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 .
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.
Oracle date data types do not support milliseconds but Oracle timestamps do.
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 .
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.
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:
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:
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 ).
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With