Oracle 特定的时间戳格式 'DD-MON-RR HH.MI.SSXFF AM'
Oracle specific timestamp format 'DD-MON-RR HH.MI.SSXFF AM'
我对 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;
备份您的 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语句。
试试这个: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
最后,我在之前的列表中添加了 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';
核心问题是,在会话级别,您有 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()
函数的第三个参数。除了所有其他正确答案之外,这也是您可以做的。
我对 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;
备份您的 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语句。
试试这个: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
最后,我在之前的列表中添加了 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';
核心问题是,在会话级别,您有 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()
函数的第三个参数。除了所有其他正确答案之外,这也是您可以做的。