1

In my application I am setting the NLS_DATE_FORMAT of the session to format all dates returned by the program using the following command:

alter session set nls_date_format='DY DDTH MON YYYY';

This should return the date like this: 'Fri 23rd Aug 2013'

However, if I run the following query:

select SYSDATE from dual;

I get the date in the following format: 'FRI 23 AUG 2013'. Notice the lack of the 'rd' after '23'.

If I run the following query:

select to_char(sysdate, 'DY DDTH MON YYYY') from dual;

The date does come back in the desired format. So why does the 'TH' date format parameter not work when setting it through NLS_DATE_FORMAT? And are there any other date format parameters that do work when using TO_CHAR but not through NLS_DATE_FORMAT?

Thanks for your help

By the way, I am using Oracle 11.2 client and database.

UPDATE

I was running the alter session command from within SQL Developer. I tried this in SQL PLUS and it returned the date in the correct format. However, now I am having a problem with inserting formatted dates.

If I do the following (after setting the NLS_DATE_FORMAT as above) I get a 'ORA-01861 literal does not match format string' error:

INSERT INTO DATE_TABLE (MY_DATE_COL) VALUES ('Thu 18th Apr 2013');

I would expect that to work after setting the NLS_DATE_FORMAT?

If I change the insert statement to:

INSERT INTO DATE_TABLE (MY_DATE_COL) VALUES ('Thu 18 Apr 2013');

Then it does work! Why is this?

4

3 回答 3

2

如果你这样做:

alter session set nls_date_format='DD DDTH MON YYYY';

你得到一个错误,ORA-01810: format code appears twice

如果您使用与您相同的格式模型,TO_CHAR那么它可以工作:

alter session set nls_date_format='DY DDTH MON YYYY';

Session altered.

select SYSDATE from dual;

SYSDATE
-----------------
FRI 23RD AUG 2013

这适用于 SQL Developer 和 SQL*Plus。


对于您关于插入的更新问题,日期时间格式文档说:

日期格式元素后缀注意事项:

  • 当您将这些后缀之一添加到日期时间格式元素时,返回值始终为英文。

  • 日期时间后缀仅对格式化输出有效。您不能使用它们将日期插入数据库。

因此,您不能显式或通过NLS_DATE_FORMAT. 您必须将其从字符串中删除或定制格式模型以将其视为固定值。

于 2013-08-23T08:34:10.633 回答
1

您在更改会话语句中有错字。首先设置 DY 而不是 DD

alter session set nls_date_format='DY DDTH MON YYYY';
于 2013-08-23T08:33:53.727 回答
1

您可以将字符串文字'Thu 18th Apr 2013'插入数据类型的列中DATE,如下所示(th用双引号括在格式模型中):

INSERT INTO DATE_TABLE (MY_DATE_COL) 
  VALUES (to_date('Thu 18th Apr 2013', 'Dy DD"th" Mon YYYY'));
于 2013-08-23T09:02:34.397 回答