簡體   English   中英

轉換和減去兩個時間列

[英]Convert and Subtract two time columns

我有一個包含2個數字列的表:start_time和seen_time。

Start_time          Seen_time 
1345                1520

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

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

只是為了好玩...不是我會為任何嚴肅的事情編碼的方式(盡管事實上,它確實正確有效地解決了問題)。 代碼與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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM