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 > Converting HH:MM:SS to seconds

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-26-11, 14:05
hpaul hpaul is offline
Registered User
 
Join Date: Jul 2011
Posts: 3
Converting HH:MM:SS to seconds

Hi all,

I was hoping to get some help with changing 'HH:MMS' to secs or minutes within my SQL code for a query. I've looked around for more then an hour online with no luck, so I hoping you guys can help.

As a reference, I'm using DB2 and it is possible for the hour to be 3 digits or more (ex. 206:23:19). Also HH:MMS is in varChar and I would like to output to be integer.

Thanks a ton for the help!
Reply With Quote
  #2 (permalink)  
Old 07-26-11, 14:11
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
I guess you could use INSTR() and SUBSTR() to parse the string, then it's simple arithmetics.
Reply With Quote
  #3 (permalink)  
Old 07-26-11, 15:35
MarkhamDBA MarkhamDBA is offline
Registered User
 
Join Date: Dec 2008
Location: Toronto, Canada
Posts: 381
may be something like this:
SECOND(TIME('your string here')) will give you seconds;

or
MINUTE(TIME('your string here')) - will give you minutes.
__________________
DB2 v9.5 ESE on AIX v6.1/ v9./10 on z/OS
Reply With Quote
  #4 (permalink)  
Old 07-26-11, 17:41
hpaul hpaul is offline
Registered User
 
Join Date: Jul 2011
Posts: 3
Thanks for the suggestions, however I still haven't been able to get it to work.

I looked into the substr. but I wasn't having luck since it converts to varchar and I can't then figure out how to change it to an integer. (I still need to later on get a sum of the time, since I have multiple records that are all related).
Although with some more help, it might work?


SECOND(TIME('your string here')) doesn't work for me either since I get a error 180 which is basically that the format doesn't conform to a valid format. I think it has to do with the ':' in the middle
Reply With Quote
  #5 (permalink)  
Old 07-26-11, 18:53
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by hpaul View Post
figure out how to change it to an integer.
May be with the help of the INTEGER() function? Or a CAST expression?
Reply With Quote
  #6 (permalink)  
Old 07-26-11, 19:46
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
I would do something like this to avoid the parsing:
Code:
VALUES HOUR('01:45:30') * 60 * 60 + MINUTE('01:45:30') * 60 + SECOND('01:45:30')
If you get SQL0180, it would be good to know what your time values look like. Because DB2 can easily parse HH:MMS format.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #7 (permalink)  
Old 07-26-11, 21:44
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
... I'm using DB2 and it is possible for the hour to be 3 digits or more (ex. 206:23:19).
If considered the additional requirement, HOUR, MINUTE , SECOND functions may get error,
and more complex expressions may be necessary.

Example 1:
Code:
------------------------------ Commands Entered ------------------------------
SELECT string_time
     , INTEGER( '0' || SUBSTR(string_time , 1 , POSSTR(string_time , ':') - 2) ) * 36000
       + MIDNIGHT_SECONDS( SUBSTR(string_time , POSSTR(string_time , ':') - 1) ) AS seconds_time
 FROM  (VALUES '206:23:19'
             ,  '13:57:09'
             ,   '1:06:05'
             ,   '0:00:00'
             ,  '72:06:05'
             , '123:46:06' ) s(string_time)
;
------------------------------------------------------------------------------

STRING_TIME SECONDS_TIME
----------- ------------
206:23:19         742999
13:57:09           50229
1:06:05             3965
0:00:00                0
72:06:05          259565
123:46:06         445566

  6 record(s) selected.
Example 2:
Code:
SELECT string_time
     , INTEGER( SUBSTR(string_time , 1              , first_sep - 1) ) * 3600
     + INTEGER( SUBSTR(string_time , first_sep  + 1 , second_sep - first_sep - 1) ) * 60
     + INTEGER( SUBSTR(string_time , second_sep + 1) ) AS seconds_time
 FROM  (VALUES '206:23:19'
             ,  '13:57:09'
             ,   '1:06:05'
             ,   '0:00:00'
             ,  '72:06:05'
             , '123:46:06'
       ) s(string_time)
 CROSS JOIN
       LATERAL
       (VALUES ( POSSTR(string_time , ':') , INSTR(string_time , ':' , 1 , 2) )
       ) sep(first_sep , second_sep)
;
------------------------------------------------------------------------------

STRING_TIME SECONDS_TIME
----------- ------------
206:23:19         742999
13:57:09           50229
1:06:05             3965
0:00:00                0
72:06:05          259565
123:46:06         445566

  6 record(s) selected.

Last edited by tonkuma; 07-26-11 at 22:13. Reason: Add data '206:23:19'
Reply With Quote
  #8 (permalink)  
Old 07-27-11, 10:00
hpaul hpaul is offline
Registered User
 
Join Date: Jul 2011
Posts: 3
Tonkuma ex. 1 worked perfectly for what I needed. Thanks a ton for the help, I appreciate it.

Thx everyone else too.
Reply With Quote
  #9 (permalink)  
Old 07-27-11, 15:06
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
This may be slightly better than Example 1.

The reasons:
1) Schema of LEFT function was SYSIBM later than DB2 9.5.
So, the performance got same level as SUBSTR.
And, length of expression using LEFT is shorter than using SUBSTR in the examples.
2) Generally speaking, integer operations show better performance than string concatenation operations.

Example 1a:
Code:
------------------------------ Commands Entered ------------------------------
SELECT string_time
     , INTEGER( LEFT(string_time , POSSTR(string_time , ':') - 1) ) / 10 * 36000
       + MIDNIGHT_SECONDS( SUBSTR(string_time , POSSTR(string_time , ':') - 1) ) AS seconds_time
 FROM  (VALUES '206:23:19'
             ,  '13:57:09'
             ,   '1:06:05'
             ,   '0:00:00'
             ,  '72:06:05'
             , '123:46:06' ) s(string_time)
;
------------------------------------------------------------------------------

STRING_TIME SECONDS_TIME
----------- ------------
206:23:19         742999
13:57:09           50229
1:06:05             3965
0:00:00                0
72:06:05          259565
123:46:06         445566

  6 record(s) selected.
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