表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作为两个时间戳格式的减法 - 结果是浮点数。 cumulativetime是字符串值。
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
答案 0 :(得分:1)
select HOURS -
cast(substr(HOURSMINUTES, 1, 2) as int) -
(cast(substr(HOURSMINUTES, 4, 2) as int) / 60.0) as diff
答案 1 :(得分: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
答案 2 :(得分: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
。