如何在Oracle中以字符串格式添加两次?

时间:2013-10-31 13:35:41

标签: string oracle datetime time oracle-sqldeveloper

我在Oracle中有两个时间字符串,格式为HH24:MI:SS。我想添加这两个并获得十进制结果(就像我们在Oracle中从另一个中减去一个日期时间后得到的结果)。有什么帮助吗?

3 个答案:

答案 0 :(得分:2)

试试这个

SELECT
      ( TO_DATE ( '13:00:00',
                'HH24:MI:SS' )
       - TO_DATE ( '12:00:00',
                'HH24:MI:SS' ) )
          AS DECIMALS,
      ( TO_DATE ( '13:00:00',
                'HH24:MI:SS' )
       - TO_DATE ( '12:00:00',
                'HH24:MI:SS' ) )
      * 24
          AS HOURS,
        ( TO_DATE ( '13:00:00',
                 'HH24:MI:SS' )
        - TO_DATE ( '12:00:00',
                  'HH24:MI:SS' ) )
      * 24
      * 60
          AS MINUTES,
        ( TO_DATE ( '13:00:00',
                 'HH24:MI:SS' )
        - TO_DATE ( '12:00:00',
                  'HH24:MI:SS' ) )
      * 24
      * 60
      * 60
          AS SECONDS
FROM
      DUAL;

<强>答案

DECIMALS            HOURS   MINUTES SECONDS
0.0416666666666667  1       60      3600

答案 1 :(得分:1)

你可以

  • 将时间字符串分为小时,分钟和秒
  • 将小时,分钟转换为秒并将其全部添加
  • 将生成的秒数转换为间隔类型
  • 以您希望的格式从间隔类型中提取时间组件。

SQL Fiddle

create table mytab(
  time1_ varchar2(8),
  time2_ varchar2(8)
  );

insert into mytab values('12:30:00','05:50:55');
insert into mytab values('18:48:56','15:33:55');

查询1

with x as (
  select time1_,
         time2_,
         substr(time1_,1,2) * 3600 +
         substr(time1_,4,2) * 60   +
         substr(time1_,7,2)            as time1_secs,
         substr(time2_,1,2) * 3600 +
         substr(time2_,4,2) * 60   +
         substr(time2_,7,2)            as time2_secs
  from mytab
  ),
y as (
  select time1_,time2_,
         numtodsinterval(time1_secs + time2_secs,'second') time_intvl
from x
  )
select time1_, time2_,
       extract(day from time_intvl) days,
       extract(hour from time_intvl) hours,
       extract(minute from time_intvl) minutes,
       extract(second from time_intvl) seconds,
       extract(day from time_intvl) * 24 +
       extract(hour from time_intvl) ||':' ||
       extract(minute from time_intvl) ||':' ||
       extract(second from time_intvl) duration
from y

<强> Results

|   TIME1_ |   TIME2_ | DAYS | HOURS | MINUTES | SECONDS | DURATION |
|----------|----------|------|-------|---------|---------|----------|
| 12:30:00 | 05:50:55 |    0 |    18 |      20 |      55 | 18:20:55 |
| 18:48:56 | 15:33:55 |    1 |    10 |      22 |      51 | 34:22:51 |

查询2

with x as (
  select time1_,
         time2_,
         numtodsinterval(
           (substr(time1_,1,2) + substr(time2_,1,2) )* 3600 +
           (substr(time1_,4,2) + substr(time2_,4,2) ) * 60  +
            substr(time1_,7,2) + substr(time2_,7,2) 
           , 'second'
           ) as time_intvl
  from mytab
  )
select time1_, time2_,
       extract(day from time_intvl) days,
       extract(hour from time_intvl) hours,
       extract(minute from time_intvl) minutes,
       extract(second from time_intvl) seconds,
       extract(day from time_intvl) * 24 +
       extract(hour from time_intvl) ||':' ||
       extract(minute from time_intvl) ||':' ||
       extract(second from time_intvl) duration
from x

<强> Results

|   TIME1_ |   TIME2_ | DAYS | HOURS | MINUTES | SECONDS | DURATION |
|----------|----------|------|-------|---------|---------|----------|
| 12:30:00 | 05:50:55 |    0 |    18 |      20 |      55 | 18:20:55 |
| 18:48:56 | 15:33:55 |    1 |    10 |      22 |      51 | 34:22:51 |

答案 2 :(得分:0)

SELECT  ROUND
        (
            ( ( TO_DATE(t1, 'HH24:MI:SS') - TO_DATE(t2, 'HH24:MI:SS') ) * 24 ) / 60 * 60
        ,   2
        ) AS t_minus
FROM
(
        SELECT  '22:10:00' AS t1
        ,       '21:05:00' AS t2
        FROM    DUAL
);