0

我在 DB2 中有表

id     HOURS     HOURSMINUTESECECONDS
1000   450.5     30:30:30

Hours 是浮点值,HOURSMINUTESSECONDS 是字符串值

我使用这个函数减去它们:

    (HOURS - 
(CAST(substr(HOURSMINUTESSECONDS , 1, 2) AS float) +
 CAST(substr(HOURSMINUTESECONDS , 4, 2) AS float)/60 + 
CAST(substr(HOURSMINUTESECONDS , 7, 2) AS float)/3600)) as diff

但是,如果我在 HOURSMINUTESECONDS 中的行在示例 300:30:30 或 3000:30:30 等上具有值,则这将不起作用(错误),因为子字符串仅采用第一个元素。

那么如何解决这个问题(SPLIT?)?要在“:”之间取全值,不计多少个字符?谢谢

4

2 回答 2

1
  • 使用LOCATE(告诉您在字符串中出现子字符串的索引)
  • 或者,引用相对于字符串末尾的冒号:

(HOURS - (CAST(substr(HOURSMINUTESSECONDS , 1, LENGTH(HOURSMINUTESSECONDS) - 6) AS float) + CAST(substr(HOURSMINUTESECONDS , LENGTH(HOURSMINUTESSECONDS) - 4, 2) AS float)/60 + CAST(substr(HOURSMINUTESECONDS , LENGTH(HOURSMINUTESSECONDS) - 1, 2) AS float)/3600)) as diff

于 2012-07-12T12:35:12.983 回答
0

可以使用MOD功能吗?

select 
    (hoursminutesseconds mod 10000) +
    ((hoursminutesseconds/100) mod 100)/60 +
    (hoursminutesseconds mod 100)*3600 
from
(
select replace(hoursminutesseconds,':','') as hoursminutesseconds from table
) as t
于 2012-07-12T12:34:03.937 回答