从Oracle 11g中的XMLType列中选择时间戳时出错

时间:2015-05-29 12:21:01

标签: oracle oracle11g timestamp xmltype timestamp-with-timezone

我有2个Oracle 11g数据库,其中包含一个包含XMLType列的表,一些测试数据仅在分隔符(。,)中与时间戳的毫秒不同:

create table TEST_TIMESTAMP (
  ID  number(19,0) constraint "NN_TEST_TIMESTAMP_ID" not null,
  DOC xmltype      constraint "NN_TEST_TIMESTAMP_DOC" not null
);

insert into TEST_TIMESTAMP values ( 1, xmltype('<?xml version="1.0" encoding="utf-8"?><test><ts>2015-04-08T04:55:33.11</ts></test>'));
insert into TEST_TIMESTAMP values ( 2, xmltype('<?xml version="1.0" encoding="utf-8"?><test><ts>2015-04-08T04:55:33,11</ts></test>'));

当我尝试使用以下语句提取时间戳时,它会在一个数据库上的第一个文档或另一个数据库上的第二个文档中失败。

select x.*
from TEST_TIMESTAMP t,
     xmltable( 
     '/test'
     passing t.DOC
     columns
       ORIGINAL varchar2(50) path 'ts',
       RESULT timestamp with time zone path 'ts'
 ) x
 where t.ID = 1;

 select x.*
 from TEST_TIMESTAMP t,
 xmltable( 
      '/test'
      passing t.DOC
      columns
        ORIGINAL varchar2(50) path 'ts',
        RESULT timestamp with time zone path 'ts'
 ) x
 where t.ID = 2;

我得到的错误:

ORA-01858: a non-numeric character was found where a numeric was expected
01858. 00000 - "a non-numeric character was found where a numeric was expected"
*Cause: The input data to be converted using a date format model was
        incorrect.  The input data did not contain a number where a number was
        required by the format model.
*Action:   Fix the input data or the date format model to make sure the
           elements match in number and type.  Then retry the operation.

我发现的数据库之间的唯一区别是:

  • DB1:version = 11.2.0.1.0,NLS_CHARACTERSET = AL32UTF8 - &gt;在文件2上失败
  • DB2:version = 11.2.0.2.0,NLS_CHARACTERSET = WE8MSWIN1252 - &gt;文件1失败

DB1具有我期望的行为。有谁知道为什么这些数据库的行为不同以及如何解决DB2中的问题?

提前致谢, 奥利弗

1 个答案:

答案 0 :(得分:1)

我的猜测是两个数据库之间的nls_timestamp_format不同。

但是,我不是在XMLTABLE级别强制隐式转换,而是在选择列表中进行显式转换:

with test_timestamp as (select 1 id, xmltype('<?xml version="1.0" encoding="utf-8"?><test><ts>2015-04-08T04:55:33.11</ts></test>') doc from dual union all
                        select 2 id, xmltype('<?xml version="1.0" encoding="utf-8"?><test><ts>2015-04-08T04:55:33,11</ts></test>') doc from dual)
select x.original,
       to_timestamp(x.original, 'yyyy-mm-dd"T"hh24:mi:ss,ff2') result
from   test_timestamp t,
       xmltable('/test' passing t.doc
                columns original varchar2(50) path 'ts') x;

ORIGINAL                                           RESULT                                            
-------------------------------------------------- --------------------------------------------------
2015-04-08T04:55:33.11                             08/04/2015 04:55:33.110000000
2015-04-08T04:55:33,11                             08/04/2015 04:55:33.110000000

N.B。我发现使用&#34; ss.ff2&#34;错了,但&#34; ss,ff2&#34;处理这两种情况都很好。不过,我不确定这是否依赖于其他一些设置。