用于减法的SQL SPLIT函数?

时间:2012-07-12 12:26:53

标签: sql split db2

我在DB2中有表

id     HOURS     HOURSMINUTESECECONDS
1000   450.5     30:30:30

小时是浮点值,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等上有值,那么这将不起作用(错误),因为substring只会占用第一个元素。

那么如何解决这个问题(SPLIT?)?要在“:”之间取得全部价值,没有多少个角色? 谢谢

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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