If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Calculate time difference between 2 timestamp fileds

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-02-04, 12:42
mgupta mgupta is offline
Registered User
 
Join Date: Oct 2003
Posts: 80
Calculate time difference between 2 timestamp fileds

I ran the sql like this.

select, TIME(FieldA), TIME(FieldB)
(Time(Field A) - Time(FieldB)) as NET

The results were

18:35:47, 18:32:45, 302

I want to show the NET as 3:02 not 302.

Please help...
Reply With Quote
  #2 (permalink)  
Old 03-02-04, 13:16
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
You probable should create a UDF to do what you want:

CREATE FUNCTION PRODUCTION.TIME_DIFF(T1 TIME,T2 TIME)
RETURNS VARCHAR(8) LANGUAGE SQL DETERMINISTIC NO EXTERNAL ACTION READS SQL DATA CALLED ON NULL INPUT
BEGIN ATOMIC
DECLARE tDIFF DECIMAL(6,0);
DECLARE T_STRING VARCHAR(8);
DECLARE HH char(2);
DECLARE MM char(2);
DECLARE SS char(2);

SET tDIFF = T1 - T2;

SET HH = SUBSTR(DIGITS(HOUR(tDIFF)),9);
SET MM = SUBSTR(DIGITS(MINUTE(tDIFF)),9);
SET SS = SUBSTR(DIGITS(SECOND(tDIFF)),9);

SET t_STRING = HH || ':' || MM || ':' || SS;

RETURN T_STRING;

END@


HTH

Andy
Reply With Quote
  #3 (permalink)  
Old 03-02-04, 13:17
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Re: Calculate time difference between 2 timestamp fileds

Quote:
Originally posted by mgupta
I ran the sql like this.

select, TIME(FieldA), TIME(FieldB)
(Time(Field A) - Time(FieldB)) as NET

The results were

18:35:47, 18:32:45, 302

I want to show the NET as 3:02 not 302.

Please help...
The result of subtracting one time value from another is timestamp duration, which is a DECIMAL(20,6) in the format:

yyyymmddhhmmss.zzzzzz, where yyyy, mm, dd, hh, mm, ss, and zzzzzz represent, respectively, the number of years, months, days, hours, minutes, seconds, and microseconds.

This basically means that if you want any fancy formatting of this number you'll have to do it yourself. For example, you could convert it to a string and then parse the string to extract minutes and seconds.
Reply With Quote
  #4 (permalink)  
Old 03-02-04, 13:23
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Re: Calculate time difference between 2 timestamp fileds

Quote:
Originally posted by n_i
The result of subtracting one time value from another is timestamp duration, which is a DECIMAL(20,6) in the format:

yyyymmddhhmmss.zzzzzz, where yyyy, mm, dd, hh, mm, ss, and zzzzzz represent, respectively, the number of years, months, days, hours, minutes, seconds, and microseconds.

This basically means that if you want any fancy formatting of this number you'll have to do it yourself. For example, you could convert it to a string and then parse the string to extract minutes and seconds.
Actually subtracting times (not timestamps) is a time duration which is DECIMAL (6,0). Subtracting timestamps is a timestamp duration as you describe.

Andy
Reply With Quote
  #5 (permalink)  
Old 03-02-04, 13:31
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Re: Calculate time difference between 2 timestamp fileds

will output from TIMESTAMPDIFF function make the customization any easier ?

Cheers
Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #6 (permalink)  
Old 03-02-04, 13:54
mgupta mgupta is offline
Registered User
 
Join Date: Oct 2003
Posts: 80
thanks for the responses.
I tried using timestampdiff. It works but the arguments options a re limited.
ie it will either give in min or seconds etc. No HH:MMS format..


anyway thanks a ton to all for the great responses.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On