Solved

# subtracting timestamp :seconds differences output as int

Posted on 2003-03-20
Medium Priority
2,617 Views
Hi I need to subtract 2 timestamps and use the result(in seconds) as an integer to be stored on a table

I have tried and then trying to convert this to a number
SELECT TO_CHAR((systimestamp - upd_tmst)*24*3600)
FROM t_process;

Outputs:

+000690855 07:34:42.355200000

TO_NUMBER will not work because of the milliseconds.

Any help appreciated.
John

0
Question by:johnnyoracle
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2
• 2
• +2

LVL 4

Expert Comment

ID: 8177779
johnnyoracle,

Try the following:

SELECT DateDiff('s',[TimeStamp1],[TimeStamp2]) AS ElapsedTime FROM TableName;

DateDiff gives you the difference between to dates, the 's' designates seconds.
0

LVL 4

Expert Comment

ID: 8177824

DateDiff gives you the difference between TWO dates, the 's' designates seconds.

CR
0

LVL 22

Accepted Solution

Helena Marková earned 180 total points
ID: 8179642
You can try this:

select to_number(to_char(systimestamp,'yyyymmddhhmisssss')-to_char(upd_tmst,'yyyymmddhhmisssss')) from t_process;

0

LVL 1

Expert Comment

ID: 8180120
I assume you are using Oracle?

Then if the times are always within the same day, convert each time into seconds:

to_number(to_char(date1,'HH24'))*3600 + to_number(date1,'MI'))*60 +_ to_char(date1,'SS')

- to_number(to_char(date2...

to get the difference in seconds

If the times will go over days, you will get a minus results and will have to add 24*60*60 to get the correct result

Suzanne
0

Author Comment

ID: 8181060
Many thanks to both Henka and Suzanne, best option is to convert to char and then subtract.in response to CRagsDell ,thanks for the help,unfortunately DateDiff is not implemented in Oracle only in Sybase.

Dates are a tricky issue, and I am probably better off writing my own function DiffSecs() etc for constant timestamp issues.

Cheers,
John

0

LVL 1

Expert Comment

ID: 8181614
I don't want to sound like a sore loser, but if you take away two times which are a minutes apart using this method you will get 100060 when presumably you would want 60?

Suzanne
0

Expert Comment

ID: 8189631
Try this out, I have just tested with a couple of dates & it seems to be working

SELECT ((TRUNC(T2-T1)*86400)-TO_CHAR(T1,'SSSSS')) + TO_CHAR(T2,'SSSSS') FROM T;

assuming t1 and t2 are two dates in table 't'
0

Expert Comment

ID: 8193437
hey, this must be more easy

SELECT (T2-T1)*3600*24  FROM T;

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
###### Suggested Courses
Course of the Month8 days, 18 hours left to enroll