?
Solved

subtracting timestamp :seconds differences output as int

Posted on 2003-03-20
8
Medium Priority
?
2,617 Views
Last Modified: 2007-12-19
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
Comment
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
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 4

Expert Comment

by:CRagsdell
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

by:CRagsdell
ID: 8177824
The last line should read:

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

CR
0
 
LVL 22

Accepted Solution

by:
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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 1

Expert Comment

by:suzieqt
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

by:johnnyoracle
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

by:suzieqt
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

by:shijee
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

by:shijee
ID: 8193437
hey, this must be more easy

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

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
Video by: Steve
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

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question