簡體   English   中英

將格式為35:23:12的VARCHAR轉換為我可以求和的數據類型

[英]Convert VARCHAR in format 35:23:12 to an datatype i can sum

我有以下格式的varchar數據:

示例1-00:00:06

示例1-372:25:27

它代表小時:分鍾:秒

我需要能夠將其轉換為數據類型,我可以對其求和,我想要單獨的小時和分鍾。 無論如何有轉換此數據,以便我可以與查詢求和?

注意,我無法更改在其他人的數據庫上執行某些工作的表。

使用正則表達式將拆分您的字符串,以便您可以使用它們

select 
regexp_substr('123:20:20' ,'(.*?)(:|$)', 1, 1, NULL, 1 ) HOUR,
regexp_substr('123:20:20' ,'(.*?)(:|$)', 1, 2, NULL, 1 ) MIN,
regexp_substr('123:20:20' ,'(.*?)(:|$)', 1, 3, NULL, 1 ) SECOND
from dual;

如@Walucas所示,您可以使用正則表達式提取字符串值的小時,分​​鍾和秒部分(假設它們的格式均相同):

select statustracking,
  regexp_substr(statustracking ,'(.*?)(:|$)', 1, 1, null, 1) as h,
  regexp_substr(statustracking ,'(.*?)(:|$)', 1, 2, null, 1) as m,
  regexp_substr(statustracking ,'(.*?)(:|$)', 1, 3, null, 1) as s
from your_table;

STATUSTRACKING H         M         S        
-------------- --------- --------- ---------
00:00:06       00        00        06       
372:25:27      372       25        27       

然后將它們轉換為數字和多個元素,以使它們全部代表秒數(例如),然后將它們加在一起得到字符串表示的秒總數:

select statustracking,
  3600 * to_number(regexp_substr(statustracking ,'(.*?)(:|$)', 1, 1, null, 1)) as h,
  60 * to_number(regexp_substr(statustracking ,'(.*?)(:|$)', 1, 2, null, 1)) as m,
  to_number(regexp_substr(statustracking ,'(.*?)(:|$)', 1, 3, null, 1)) as s,
  3600 * to_number(regexp_substr(statustracking ,'(.*?)(:|$)', 1, 1, null, 1))
    + 60 * to_number(regexp_substr(statustracking ,'(.*?)(:|$)', 1, 2, null, 1))
    + to_number(regexp_substr(statustracking ,'(.*?)(:|$)', 1, 3, null, 1)) as total_s
from your_table;

STATUSTRACKING          H          M          S    TOTAL_S
-------------- ---------- ---------- ---------- ----------
00:00:06                0          0          6          6
372:25:27         1339200       1500         27    1340727

然后將這些總數相加:

select sum(
    3600 * to_number(regexp_substr(statustracking ,'(.*?)(:|$)', 1, 1, null, 1))
      + 60 * to_number(regexp_substr(statustracking ,'(.*?)(:|$)', 1, 2, null, 1))
      + to_number(regexp_substr(statustracking ,'(.*?)(:|$)', 1, 3, null, 1))
  ) as total_s
from your_table;

   TOTAL_S
----------
   1340733

然后反轉轉換過程,將總秒數分解為小時,分鍾和(如果需要)秒數:

select floor(total_s / 3600) as h,
       floor(mod(total_s, 3600) / 60) as m,
       mod(total_s, 60) as s
from (
  select sum(
      3600 * to_number(regexp_substr(statustracking ,'(.*?)(:|$)', 1, 1, null, 1))
        + 60 * to_number(regexp_substr(statustracking ,'(.*?)(:|$)', 1, 2, null, 1))
        + to_number(regexp_substr(statustracking ,'(.*?)(:|$)', 1, 3, null, 1))
    ) as total_s
  from your_table
);

         H          M          S
---------- ---------- ----------
       372         25         33

如果您想返回字符串格式,則可以將它們與冒號連接在一起,並使用lpad()to_char()用零左填充:

select to_char(total_s / 3600, 'FM9999900')
       ||':'|| to_char(mod(total_s, 3600) / 60, 'FM00')
       ||':'|| to_char(mod(total_s, 60), 'FM00')
       as total_time
from (
  select sum(
      3600 * to_number(regexp_substr(statustracking ,'(.*?)(:|$)', 1, 1, null, 1))
        + 60 * to_number(regexp_substr(statustracking ,'(.*?)(:|$)', 1, 2, null, 1))
        + to_number(regexp_substr(statustracking ,'(.*?)(:|$)', 1, 3, null, 1))
    ) as total_s
  from your_table
);

TOTAL_TIME      
----------------
372:26:33

分貝<>小提琴

暫無
暫無

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

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