Oracle NUMBER到TIME

时间:2015-09-28 13:45:43

标签: oracle plsql

我正在编写一些ETL来从遗留的Oracle数据库和SQL Server数据库中获取数据,以获取Analysis服务等。

旧版oracle数据库将多个时间列存储为Number(4,2)。

9 = 09:00
1.2 = 01:20
11.53 = 11:53

是否有任何内置函数可将其转换为Time数据类型?有没有人遇到过这个?你是怎么解决的?

由于

2 个答案:

答案 0 :(得分:1)

Oracle没有TIME数据类型 - 它有DATETIMESTAMP,它们都有日期和时间组件。

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE times ( time ) AS
          SELECT  9    FROM DUAL
UNION ALL SELECT  1.2  FROM DUAL
UNION ALL SELECT 15.53 FROM DUAL
UNION ALL SELECT 24.62 FROM DUAL;

查询1 - 为有效时间的数字获取正确的时间分量:

SELECT time,
       TO_DATE( TO_CHAR( time, '00.00' ), 'HH24.MI' ) AS parsed_time
FROM   times
WHERE  REGEXP_LIKE( TO_CHAR( time, '00.00' ), '([01]\d|2[0-3]).[0-5]\d' )

<强> Results

|  TIME |                 PARSED_TIME |
|-------|-----------------------------|
|     9 | September, 01 2015 09:00:00 |
|   1.2 | September, 01 2015 01:20:00 |
| 15.53 | September, 01 2015 15:53:00 |

查询2 - 为今日有效时间的数字获取正确的时间组件

SELECT time,
       TO_DATE( TO_CHAR( SYSDATE, 'YYYY-MM-DD' ) || ' ' || TO_CHAR( time, '00.00' ), 'YYYY-MM-DD HH24.MI' ) AS parsed_time
FROM   times
WHERE  REGEXP_LIKE( TO_CHAR( time, '00.00' ), '([01]\d|2[0-3]).[0-5]\d' )

<强> Results

|  TIME |                 PARSED_TIME |
|-------|-----------------------------|
|     9 | September, 28 2015 09:00:00 |
|   1.2 | September, 28 2015 01:20:00 |
| 15.53 | September, 28 2015 15:53:00 |

查询3 - 获取任意小时数(单位部分)和分钟(小数部分)的正确时间分量

SELECT time,
       TRUNC( SYSDATE ) + TRUNC(time)/24 + (time - TRUNC(time)) * 100 / 60/ 24 AS parsed_time
FROM   times

<强> Results

|  TIME |                 PARSED_TIME |
|-------|-----------------------------|
|     9 | September, 28 2015 09:00:00 |
|   1.2 | September, 28 2015 01:20:00 |
| 15.53 | September, 28 2015 15:53:00 |
| 24.62 | September, 29 2015 01:02:00 |

答案 1 :(得分:0)

EDIT2:根据MT0更正了查询。如果您之间有:而不是.

,这也会考虑到这种情况
        with tbl as(
        select 9 as tf from dual union all
        select 1.2 from dual union all
        select 11.53 as tf from dual 
        )
        select TO_DATE( TO_CHAR( replace(to_char(tf),':','.'), '00.00' ), 'HH24.MI' ) AS time from tbl

EDIT1:它只适用于您提供的值。如果你的某些东西不是一个有效的时间,那么它将无法说

 ORA-01850: hour must be between 0 and 23
 or
 ORA-01851: minutes must be between 0 and 59