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 > Calculating the Hours between two time values in decimal

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-24-10, 08:26
dinesh1985 dinesh1985 is offline
Registered User
 
Join Date: Sep 2010
Posts: 6
Calculating the Hours between two time values in decimal

Dear Folks,
I want to calculate the elapsed hours between two time columns. I am using timestampdiff method for the same. I am able to get the value. But facing an issue of decimal values. For example the elapsed hours between 09:00:00 and 20:30:00 is coming as 11 instead of 11.5. I am using below syntax to get the output.

Code:
select
T_OPEN_TIME,
T_CLSNG_TIME,
TIMESTAMPDIFF
(
8,
char
(
TIMESTAMP(CURRENT date,T_CLSNG_TIME)
-
TIMESTAMP(CURRENT date,T_OPEN_TIME)
)
)
as elapsed_hours
from HOURS_TABLE
Please help me with this
Reply With Quote
  #2 (permalink)  
Old 09-24-10, 08:54
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Please try:
( MIDNIGHT_SECONDS(T_CLSNG_TIME) - MIDNIGHT_SECONDS(T_OPEN_TIME) ) / 3600.

Note: don't forget decimal point(".").
Reply With Quote
  #3 (permalink)  
Old 09-24-10, 09:57
dinesh1985 dinesh1985 is offline
Registered User
 
Join Date: Sep 2010
Posts: 6
Thanks a lot

Quote:
Originally Posted by tonkuma View Post
Please try:
( MIDNIGHT_SECONDS(T_CLSNG_TIME) - MIDNIGHT_SECONDS(T_OPEN_TIME) ) / 3600.

Note: don't forget decimal point(".").

Thanks a lot ..its working perfectly
Reply With Quote
  #4 (permalink)  
Old 10-04-10, 08:17
dinesh1985 dinesh1985 is offline
Registered User
 
Join Date: Sep 2010
Posts: 6
Need a help further

Hi tonkuma,
I am using the below query and I am getting the proper result.
Code:
select
T_OPEN_TIME,
T_CLSNG_TIME,
TIMESTAMPDIFF
(
4,
char
(
TIMESTAMP(CURRENT date,T_CLSNG_TIME)
-
TIMESTAMP(CURRENT date,T_OPEN_TIME)
)/60.
)
as elapsed_hours
from HOURS_TABLE
but when I am exporting the data to a file, I am getting the values
+9.50000000000000000000 instead of 9.5.When I tried to cast as decimal(2,1),I am getting stack overflow error. Can you please give me some help on this
Reply With Quote
  #5 (permalink)  
Old 10-05-10, 01:01
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
but when I am exporting the data to a file, I am getting the values
+9.50000000000000000000 instead of 9.5.
I don't know exactly the functionality of EXPORT command.

Quote:
When I tried to cast as decimal(2,1),I am getting stack overflow error.
Please study the meaning of first number(n) and second number(m) in data type DEC(n , m).


Why do you want to use TIMESTAMPDIFF function?
It's get longer than my example using MIDNIGHT_SECONDS functions.
Reply With Quote
Reply

Tags
db2, hours, time

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