0

我有 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:版本=11.2.0.1.0,NLS_CHARACTERSET=AL32UTF8 -> 在文档 2 上失败
  • DB2:版本=11.2.0.2.0,NLS_CHARACTERSET=WE8MSWIN1252 -> 在文档 1 上失败

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

在此先感谢,奥利弗

4

1 回答 1

2

我的猜测是两个数据库之间的 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

注意我发现使用“ss.ff2”会出错,但“ss,ff2”处理这两种情况都很好。不过,我不确定这是否依赖于其他一些 nls 设置。

于 2015-05-29T14:48:46.660 回答