我遇到了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;
答案 0 :(得分:5)
核心问题是,在会话级别,您有nls_numeric_characters=',.'
而您的时间戳字符串包含点(.
),而不是秒 - 从 - 微秒分隔符。
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>
请注意第三个SELECT中to_timestamp()
函数的第三个参数。除了所有其他正确答案之外,这也是你能做的。
答案 1 :(得分: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语句。
答案 2 :(得分:0)
最后,我在之前的列表中添加了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';
答案 3 :(得分:-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