首页 > 代码库 > TIMESAMPE_HEX_TO_CHAR函数 解决时间16进制可视化问题

TIMESAMPE_HEX_TO_CHAR函数 解决时间16进制可视化问题

 
在11G 提供了一个超过4秒的SQL视图 里面包含了很多必要的信息 同时可以使用

--查看超过4秒语句执行计划
SELECT dbms_sqltune.report_sql_monitor(sql_id => ‘&sql_id‘,report_level => ‘ALL‘,type=>‘TEXT‘) comm FROM dual;

获得个格式美化的东西

 

可是其中

Binds
========================================================================================================================
| Name | Position |   Type    |                                         Value                                          |
========================================================================================================================
| :1   |        1 | TIMESTAMP | 78720C0D010101                                                                         |
| :2   |        2 | TIMESTAMP | 78720C0D183C3C                                                                         |
| :3   |        3 | NUMBER    | 3                                                                                      |
| :4   |        4 | NUMBER    | 10000978                                                                               |
| :5   |        5 | TIMESTAMP | 78720C0D010101                                                                         |
| :6   |        6 | TIMESTAMP | 78720C0D183C3C                                                                         |
| :7   |        7 | NUMBER    | 10000978                                                                               |
| :8   |        8 | NUMBER    | 2                                                                                      |
| :9   |        9 | NUMBER    | 1000                                                                                   |
| :10  |       10 | NUMBER    | 0                                                                                      |
========================================================================================================================

TIMESTAMP 却是16进制.

 

如何让它变得萌萌哒呢?

如果简单的16进制换算成10进制 我们可以用WINDOWS的计算器来玩, 可是这是时间啊,ORACLE时间存储规则不一样哦.对了其实也是数字存储方式的

另外还有正负数,小数,指数.

当然我们这里个时间类型 包含 年份 日期 时间 毫秒.  这四个部分算法是不一样的.

你可以SELECT DUMP(SYSDATE) FROM DUAL 出来对比了解下

 

啰嗦 上函数. 不够这函数解决年月日和时间

create or replace function hex_to_date(hexstr in varchar2) return timestamp as
begin
  if length(hexstr) <> 14 then
    return null;
  end if;

  return to_timestamp(to_char(to_number(substr(hexstr, 1, 2), ‘xx‘) - 100,
                              ‘fm00‘) ||
                      to_char(to_number(substr(hexstr, 3, 2), ‘xx‘) - 100,
                              ‘fm00‘) ||
                      to_char(to_number(substr(hexstr, 5, 2), ‘xx‘),
                              ‘fm00‘) ||
                      to_char(to_number(substr(hexstr, 7, 2), ‘xx‘),
                              ‘fm00‘) ||
                      to_char(to_number(substr(hexstr, 9, 2), ‘xx‘) - 1,
                              ‘fm00‘) ||
                      to_char(to_number(substr(hexstr, 11, 2), ‘xx‘) - 1,
                              ‘fm00‘) ||
                      to_char(to_number(substr(hexstr, 13, 2), ‘xx‘) - 1,
                              ‘fm00‘),
                      ‘yyyymmddhh24miss‘);
end;


 

TIMESAMPE_HEX_TO_CHAR函数 解决时间16进制可视化问题