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

问题描述 投票:0回答:2

我有 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 中的问题?

提前致谢, 奥利弗

oracle oracle11g timestamp xmltype timestamp-with-timezone
2个回答
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 设置。


0
投票

我发现XMLTable忽略nls_timestamp_tz_format并且需要ISO TS…除了由nls_numeric_characters处理的毫秒部分

所以 2 会在 a 后工作

alter session set nls_numeric_characters = ',.';

而 1 需要一个

alter session set nls_numeric_characters = '.,';
© www.soinside.com 2019 - 2024. All rights reserved.