2012-07-12 60 views
1

表DB2SQL如何減去浮點時間和字符串時間?

ID  HOURS  HOURSMINUTES  
1000  480.5  30:30:00 

我想HOURS - HOURSMINUTES

ID  HOURS - HOURSMINUTES 
1000  450.0 

小時在小時 - float,因此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 

我也試過卡皮爾版本:

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 
+1

你可以試試嗎? \t'選擇\t t1.id, \t \t \t DEC((TIMESTAMPDIFF(4,焦炭(t1.actualfinish - t1.reportdate))/ 60.00),10,2),爲小時, \t \t \t t2.cumulativetime如HOURSMINUTES, \t \t \t鑄造(SUBSTR(t2.cumulativetime,1,2)爲INT)作爲hhdif, \t \t \t(鑄造(SUBSTR(t2.cumulativetime,4,2),爲INT)/ 60.0)作爲mmdiff \t from \t t1在t2.id = t1.id處加入t2 請發佈輸出或錯誤消息 – paul 2012-07-12 12:03:26

+0

paul我成功的問題是,我的專欄中的一個值是512:30:30所以substr(1,2)(4,2)它採取無效值(「:」被包含)。那麼如何解決呢?在HOURSMINUTES列中,如果小數點後的小數位數字需要幾小時才能使用,謝謝 – Dejan 2012-07-12 12:10:05

回答

1
select HOURS - 
     cast(substr(HOURSMINUTES, 1, 2) as int) - 
     (cast(substr(HOURSMINUTES, 4, 2) as int)/60.0) as diff 
+0

op標記的DB2,所以'substr'而不是'substring' – paul 2012-07-12 11:07:01

+0

感謝您的。更正它。 – 2012-07-12 11:08:56

+0

嘿它的工作,但我有一個問題 – Dejan 2012-07-12 12:05:01

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 
+0

它的工作原理,但我有一個問題 – Dejan 2012-07-12 12:04:48

+0

在這種情況下,我的HOURSMINUTES列以30(小數點後兩位)開頭,但我有表值1,2,3,4,5,6,7,8,9小數位我不能使用這個查詢,因爲「:」將作爲浮點值進行減法的一部分,因此無效 – Dejan 2012-07-12 12:07:03

+0

那麼如何解決這個問題,以便在HOURSMINUTES列中爲所有值保留數小時而獨一無二? – Dejan 2012-07-12 12:07:28

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可用,則可以使用它來簡化找到HMS字符串中的第n個: