2016-12-27 176 views
1

我有一個包含2個數字列的表:start_time和seen_time。轉換和減去兩個時間列

Start_time   Seen_time 
1345    1520 

這意味着開始時間是下午1:45和seen_time是下午3:20

我想減去列和結果是1:35^h

+0

你試過了DATEDIFF()函數嗎? –

+0

請您介意寫出它的語法請 –

+0

請顯示錶格定義,列數據類型以及完整的樣本數據。 oracle中沒有這樣的「時間」類型。有一個DATE類型,它包含一個類型組件,所以你的問題不清楚。 – OldProgrammer

回答

0

只是爲了好玩...不這是一種我認真編寫代碼的方法(但實際上,它確實正確並且有效地解決了問題)。該代碼與OP的數據模型一樣不尋常(意思是「不要做這些重要的事情」)。

with 
    inputs (time_from, time_to) as (
     select 32, 233 from dual union all 
     select 1030, 2322 from dual union all 
     select 2200, 1130 from dual union all 
     select 2030, 3544 from dual union all 
     select 1233, 2051 from dual union all 
     select 1215, 1360 from dual 
    ) 
-- end of test data; solution (SQL query) begins below this line 
select time_from, time_to, 
     case when time_from > time_to then 'Error: time_from > time_to' 
      when trunc(time_from/100) > 23 then 'Error: invalid hours in time_from' 
      when mod (time_from , 100) > 59 then 'Error: invalid minutes in time_from' 
      when trunc(time_to /100) > 23 then 'Error: invalid hours in time_to' 
      when mod (time_to , 100) > 59 then 'Error: invalid minutes in time_to' 
      else to_char(time_to - time_from - 
        case when mod(time_to, 100) < mod(time_from, 100) then 40 else 0 end, 
        'FM00G00L', 'nls_numeric_characters='',:'' nls_currency='' h''') 
      end as time_diff 
from inputs 
; 

TIME_FROM TIME_TO TIME_DIFF 
---------- -------- ----------------------------------- 
     32  233 02:01 h 
     1030  2322 12:52 h 
     2200  1130 Error: time_from > time_to 
     2030  3544 Error: invalid hours in time_to 
     1233  2051 08:18 h 
     1215  1360 Error: invalid minutes in time_to 

6 rows selected.