Oracle 11g Sql 从 blob 字段转换日期

Oracle 11g Sql convert date from blob field

我在 Oracle 11g sql 命令中转换存储在 blob 字段中的日期值时遇到问题。当我执行 sql:

select dump(HIGH_VALUE) from all_tab_columns where COLUMN_NAME='TARIH'

我收到以下结果;

Typ=23 Len=7: 120,116,3,6,1,1,1

我知道这些数字代表一个日期(不是日期时间),但我不知道如何从这个结果中提取日期。

提前致谢, 阿尔珀

甲骨文stores dates in tables as 7-bytes

byte 1 - century + 100
byte 2 - (year MOD 100 ) + 100
byte 3 - month
byte 4 - day
byte 5 - hour + 1
byte 6 - minute + 1
byte 7 - seconds+ 1

因此 120,116,3,6,1,1,1 转换为:

byte 1 - century = 120 - 100 = 20
byte 2 - year = 116 - 100 = 16
byte 3 - month = 3
byte 4 - day = 6
byte 5 - hour = 1 - 1 = 0
byte 6 - minute = 1 - 1 = 0
byte 7 - seconds = 1 - 1 = 0

所以2016-03-06T00:00:00

Oracle 设置:

CREATE TABLE file_upload ( file_blob BLOB );

INSERT INTO file_upload VALUES (
  utl_raw.cast_to_raw(
    CHR(120) || CHR(116) || CHR(3) || CHR(6) || CHR(1) || CHR(1) || CHR(1)
  )
);

查询:

SELECT DUMP( DBMS_LOB.SUBSTR( file_blob, 7, 1 ) ) AS dmp,
       TO_DATE(
         TO_CHAR(
           ( ASCII( SUBSTR( chars, 1, 1 ) ) - 100 ) * 100
             + ASCII( SUBSTR( chars, 2, 1 ) ) - 100,
           '0000'
         )
         || TO_CHAR( ASCII( SUBSTR( chars, 3, 1 ) ), '00' )
         || TO_CHAR( ASCII( SUBSTR( chars, 4, 1 ) ), '00' )
         || TO_CHAR( ASCII( SUBSTR( chars, 5, 1 ) ) - 1, '00' )
         || TO_CHAR( ASCII( SUBSTR( chars, 6, 1 ) ) - 1, '00' )
         || TO_CHAR( ASCII( SUBSTR( chars, 7, 1 ) ) - 1, '00' ),
         'YYYYMMDDHH24MISS'
       ) AS converted_date
FROM   (
  SELECT file_blob,
         UTL_RAW.CAST_TO_VARCHAR2(DBMS_LOB.SUBSTR( file_blob, 7, 1 ) ) AS chars
  FROM   file_upload
);

输出:

DMP                             CONVERTED_DATE    
------------------------------- -------------------
Typ=23 Len=7: 120,116,3,6,1,1,1 2016-03-06 00:00:00