1

表 DB2

ID       HOURS        HOURSMINUTES       
1000     480.5        30:30:00

我想得到 HOURS - HOURSMINUTES

ID       HOURS - HOURSMINUTES
1000       450.0

HOURS 是 HOURS 中的浮点值 - 所以 480.5 小时。HOURSMINUTES 是字符串值:30:30:00(30 小时 30 分 00 秒)

怎么减?

这是我的完整表达,因为我从两个表中获取值(我以这种格式获取它们,但我不能减去)。我已经从两种时间戳格式中减去 HOURS - 结果是浮点数。累积时间是字符串值。

select t1.id,
dec (( timestampdiff(
  4, 
  char(t1.actualfinish - t1.reportdate))/60.00),10,2) as HOURS,t2.cumulativetime as HOURSMINUTES
from t1
join t2 on t2.id=t1.id 

当我尝试在下面插入解决方案时,出现错误。

    select t1.id,
    dec (( timestampdiff(
      4, 
      char(t1.actualfinish - t1.reportdate))/60.00),10,2) as HOURS,t2.cumulativetime as HOURSMINUTES,

dec (( timestampdiff(
      4, 
      char(t1.actualfinish - t1.reportdate))/60.00),10,2)- cast(substr(t2.cumulativetime, 1, 2) as int) - 
      (cast(substr(t2.cumulativetime, 4, 2) as int) / 60.0) as diff
    from t1
    join t2 on t2.id=t1.id 

我也试过 Kapil 版本:

select t1.id,
    dec (( timestampdiff(
      4, 
      char(t1.actualfinish - t1.reportdate))/60.00),10,2) as HOURS,t2.cumulativetime as HOURSMINUTES,

(dec (( timestampdiff(
  4, 
  char(t1.actualfinish - t1.reportdate))/60.00),10,2) - (CAST(substr(t2.cumulativetime , 1, 2) AS float) + CAST(substr(t2.cumulativetime , 4, 2) AS float)/60 + CAST(substr(t2.cumulativetime , 7, 2) AS float)/3600)) as diff

from t1
    join t2 on t2.id=t1.id 
4

3 回答 3

1
select HOURS - 
       cast(substr(HOURSMINUTES, 1, 2) as int) - 
      (cast(substr(HOURSMINUTES, 4, 2) as int) / 60.0) as diff
于 2012-07-12T11:04:40.163 回答
1

尝试这个:

Select  (HOURS - (CAST(substr(HOURSMINUTES , 1, 2) AS float) + CAST(substr(HOURSMINUTES , 4, 2) AS float)/60 + CAST(substr(HOURSMINUTES , 7, 2) AS float)/3600)) as diff
From table
于 2012-07-12T11:13:18.577 回答
1

以下是在兼容模式下使用 DB2 for z/OS 9.1 版的解决方案:

select
  t.HRS
  - cast(substr(t.HMS, 1, locate(':', t.HMS) - 1) as FLOAT)
  - (cast(substr(t.HMS, locate(':', t.HMS) + 1, locate(':', t.HMS, locate(':', t.HMS) + 1) - locate(':', t.HMS) - 1) as FLOAT) / 60.0)
  - (cast(substr(t.HMS, locate(':', t.HMS, locate(':', t.HMS, locate(':', t.HMS) + 1)) + 1) as FLOAT) / 3600.0)
from
  (
    select
      cast(480.5 as float) as HRS
      , '333:44:55' as HMS
    from
      sysibm.SYSDUMMY1
  ) as t
with ur for read only;

这给出了 的结果146.75138888888887

如果有可用的,您可以使用它来简化在字符串中LOCATE_IN_STRING查找第 n 个的过程。:HMS

于 2012-07-12T13:00:06.377 回答