转换和减去两个时间列

时间:2016-12-27 19:33:51

标签: sql oracle

我有一个包含2个数字列的表:start_time和seen_time。

Start_time          Seen_time 
1345                1520

这意味着开始时间是下午1:45,see_time是下午3:20

我想减去列数,结果为1:35 h

1 个答案:

答案 0 :(得分:0)

只是为了好玩...不是一种方法我会编码任何严肃的事情(虽然,事实上,它确实正确和有效地解决了问题)。代码与OP的数据模型一样不同寻常(意思是,#34;不要做任何重要事情")。

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.