I am struggling with an SQL time comparison on two fields that have been set up as integers, rather than time stamps.
I have some performance metrics in a database that the developer originally set up as int(8). The database contains the start and end times of a transaction. For example
Some sample data may be
id | start_time | end_time
---------------------------
1 | 85958 | 90001
If I simply subtracted the two values, I would get 4043 seconds, when the transaction time is only 3. I am struggling however to convert the values into a time format that allows me to perform date comparisons.
I am unable to do this calculation within the application, because there are 100’s of rows in the database per day, and I am trying to calculate the average and max times for the transactions.
EDIT:
To clarify
The times are in seconds
85958 represent 8:59:58 90001 represents 9:00:01
To make things worse, 1 minute past midnight 0:01:00 would be represented as 100.
Most of the answers already described are all valid, but unfortunately the iSeries seems to quibble over different functions and so I had to tailor the answers given to get it to work.
The final solution I got was
select
TIME(SUBSTR(DIGITS(END_TIME),1,2) CONCAT ':' CONCAT SUBSTR(DIGITS(END_TIME),3,2) CONCAT ':' CONCAT SUBSTR(DIGITS(END_TIME),5,2)) - TIME(SUBSTR(DIGITS(START_TIME),1,2) CONCAT ':' CONCAT SUBSTR(DIGITS(START_TIME),3,2) CONCAT ':' CONCAT SUBSTR(DIGITS(START_TIME),5,2))
from table1;
Thanks for all the quick and detailed responses