?
Solved

subtracting timestamp :seconds differences output as int

Posted on 2003-03-20
8
Medium Priority
?
2,628 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
  • 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
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!

 
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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
Suggested Courses

616 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