i need to find the difference between the time in the format hh:mm:ss
select msglog.id,max(msglog.timestamp) enddate,
min(msglog.timestamp) startdate,
enddate - startdate
from MESSAGELOG msglog
group by id
In the abovequery msglog.timestamp is of type DATE.
How can I get the elapsed time or diff between the time in the correct format in oracle?
Discussion: 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 .
To calculate the difference between the arrival and the departure in T-SQL, use the DATEDIFF(datepart, startdate, enddate) function. The datepart argument can be microsecond , second , minute , hour , day , week , month , quarter , or year . Here, you'd like to get the difference in seconds, so choose second.
Elapsed_time is the total time the query takes*. This includes the CPU time + waits (I/O, network, etc.). The elapsed time for a given execution should match the duration provided by SQL monitor.
How can I get the elapsed time or diff between the time in the correct format in oracle? Show activity on this post. When you subtract two DATE values like enddate - startdate you get the difference in days with decimal accuracy, so for example 1.5 would mean 1 1/2 days or 36 hours.
Since taking the difference between two dates in Oracle returns the difference in fractional days, you need to multiply the result by 1440 to translate your result into elapsed minutes. 24 * 60 = 1440 24 hours in a day * 60 minutes in an hour
Question: In Oracle, how can I retrieve the total elapsed time in minutes between two dates? Answer: To retrieve the total elapsed time in minutes, you can execute the following SQL:
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.
When you subtract two DATE
values like enddate - startdate
you get the difference in days with decimal accuracy, so for example 1.5 would mean 1 1/2 days or 36 hours. You can convert that to HH:MI:SS
using a lot of math, but an easier way is to convert the decimal value to an INTERVAL DAY TO SECOND
value using the NUMTODSINTERVAL
function:
NUMTODSINTERVAL(enddate - startdate, 'DAY')
You'd think the TO_CHAR
function would be able to format this as HH:MI:SS
, but it doesn't seem to work that way. You can use EXTRACT
instead, and TO_CHAR
to make sure you get leading zeros:
TO_CHAR(EXTRACT(HOUR FROM NUMTODSINTERVAL(enddate-startdate, 'DAY')), 'FM00')
|| ':' ||
TO_CHAR(EXTRACT(MINUTE FROM NUMTODSINTERVAL(enddate-startdate, 'DAY')), 'FM00')
|| ':' ||
TO_CHAR(EXTRACT(SECOND FROM NUMTODSINTERVAL(enddate-startdate, 'DAY')), 'FM00')
The 00
part of the format code specifies two digits, with a leading zero if needed. The FM
part gets rid of the leading space in the formatted result, which is reserved for a negative sign if needed.
Also note that your query gets aggregate values and uses them in the same SELECT
list. Oracle won't let you do this. Try something like this instead:
WITH StartEndByID AS (
SELECT
msglog.id,
NUMTODSINTERVAL(max(msglog.timestamp) - min(msglog.timestamp), 'DAY') elapsed
FROM messagelog msglog
GROUP BY id
)
SELECT
id,
TO_CHAR(EXTRACT(HOUR FROM elapsed), 'FM00') || ':' ||
TO_CHAR(EXTRACT(MINUTE FROM elapsed), 'FM00') || ':' ||
TO_CHAR(EXTRACT(SECOND FROM elapsed), 'FM00') AS ElapsedHHMISS
FROM StartEndByID
Date arithmetic in Oracle results in a number expressed in days. So, to convert to hours, you would multiply by 24 and then trunc
to get an integral number:
trunc(24 * (enddate - startdate))
To get minutes, convert the days value to minutes and mod()
that with 60:
mod(trunc(24 * 60 * (enddate - startdate)), 60)
For seconds, again convert days to seconds and mod()
that with 60:
mod(trunc(24 * 60 * 60 * (enddate - startdate)), 60)
Now you can put these together to get the string value you need.
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