5

我对 Oracle 11g 特定的时间戳格式有疑问。

这就是我所拥有的: select to_timestamp('21-OCT-15 08.24.30.000000000 PM','DD-MON-RR HH.MI.SSXFF AM') from dual;

来自数据库的响应:ORA-01855: AM/A.M. or PM/P.M. required 01855. 00000 - "AM/A.M. or PM/P.M. required"

我还尝试使用几个命令更改会话设置,但仍然没有。

alter session set NLS_LANGUAGE='ENGLISH';
alter session set NLS_DATE_LANGUAGE='ENGLISH';
alter session set NLS_TIMESTAMP_FORMAT = 'DD-MON-RR HH.MI.SSXFF AM';
alter session set NLS_TIMESTAMP_TZ_FORMAT='DD-MON-RR HH.MI.SSXFF AM';

我无法在 SELECT 语句中更改时间戳格式,需要保持原样。我想问题出在会话设置中。

有Oracle数据库管理经验的人可以提出一些建议,我会尝试的。我知道有几个类似的帖子,但我没有找到解决方案。谢谢

这是我的会话设置。

select * from nls_session_parameters;

会话参数

4

4 回答 4

6

核心问题是,在会话级别,您nls_numeric_characters=',.'的时间戳字符串包含点 ( .) 作为 seconds-from-microseconds 分隔符。

to_timestamp()函数可以接受用于覆盖 NLS 设置的第三个参数。这是给你的一个小演示......

Connected to Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 
Connected as ******@******

--- This is how it behaves in your database (with "," as the decimals separator) ...

SQL> alter session set nls_numeric_characters = ',.';

Session altered

SQL> select to_timestamp('21-OCT-15 08.24.30.000000000 PM','DD-MON-RR HH.MI.SSXFF AM') as xx from dual;

select to_timestamp('21-OCT-15 08.24.30.000000000 PM','DD-MON-RR HH.MI.SSXFF AM') as xx from dual

ORA-01855: AM/A.M. or PM/P.M. required

--- This is how it behaves in my database (with "." as the decimals separator) ...

SQL> alter session set nls_numeric_characters = '. ';

Session altered

SQL> select to_timestamp('21-OCT-15 08.24.30.000000000 PM','DD-MON-RR HH.MI.SSXFF AM') as xx from dual;

XX
-------------------------------------------------
21.10.15 20:24:30.000000000

--- Now back to your database settings and let's make the conversion NLS-settings-indepenent ...

SQL> alter session set nls_numeric_characters = ',.';

Session altered

SQL> select to_timestamp('21-OCT-15 08.24.30.000000000 PM','DD-MON-RR HH.MI.SSXFF AM', 'nls_numeric_characters = ''. ''') as xx from dual;

XX
-------------------------------------------------
21.10.15 20:24:30,000000000

SQL> 

请注意第三to_timestamp()个 SELECT 中函数的第三个参数。除了所有其他正确答案之外,您也可以这样做。

于 2015-11-25T12:14:03.593 回答
1

最后,我在上一个列表中添加了 Territory,更改了会话属性以使其对我有用。谢谢

ALTER SESSION SET NLS_TERRITORY=AMERICA;
ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';
ALTER SESSION SET NLS_TIME_FORMAT='HH.MI.SSXFF AM';
ALTER SESSION SET NLS_TIMESTAMP_FORMAT='DD-MON-RR HH.MI.SSXFF AM';
ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT='DD-MON-RRRR HH.MI.SSXFF AM TZR';
ALTER SESSION SET NLS_TIME_TZ_FORMAT='HH.MI.SSXFF AM TZR';
于 2015-11-25T10:26:19.517 回答
1

备份您的 NLS 设置并运行这些设置。

    ALTER SESSION SET NLS_DATE_FORMAT='DD-MON-RRRR HH24:MI:SS';
    ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';
    ALTER SESSION SET NLS_TIME_FORMAT='HH.MI.SSXFF AM';
    ALTER SESSION SET NLS_TIMESTAMP_FORMAT='DD-MON-RR HH.MI.SSXFF AM';
    ALTER SESSION SET NLS_TIME_TZ_FORMAT='HH.MI.SSXFF AM TZR';
    ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT='DD-MON-RRRR HH.MI.SSXFF AM TZR';

然后再次运行 SQL 语句。

于 2015-11-21T12:54:44.887 回答
-1

试试这个:SELECT TO_CHAR(TO_TIMESTAMP(**'2015-11-21-13:03:07.04776'**),'DD-MON-RR HH.MI.SSXFF AM') FROM DUAL;引号之间的时间戳格式应该与这个查询的结果相同:SELECT TO_CHAR(LOCALTIMESTAMP) FROM DUAL

于 2015-11-21T13:10:22.057 回答