0

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

4

3 回答 3

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
于 2013-10-31T13:42:02.873 回答
1

你可以

  • 将时间字符串拆分为小时、分钟和秒
  • 将小时、分钟转换为秒并将其全部加起来
  • 将生成的秒数转换为间隔类型
  • 以您想要的格式从间隔类型中提取时间分量。

SQL小提琴

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

结果

|   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

结果

|   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 |
于 2013-11-01T02:37:57.700 回答
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
);
于 2013-10-31T15:20:25.900 回答