2016-12-27 193 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.