我对理解一个问题有疑问。
假设参数nls_date_format
设置为dd-mon-rr
,
然后,如果有一个包含日期类型的列的表,我只能在语句如下所示插入数据:
insert into table values ('01-jan-00')
或'01 -january-00'而不是jan。无法插入日期01-01-00(月份无效)。
在另一个网站上,当nls_date_format参数设置为dd-mm-rr
时,我可以以'01 -jan-00'或'01 -01-01'或甚至'01 -january-00的格式插入日期'。没有无效的月份错误。那么它是如何工作的,oracle可以将日期从字符串(1月)转换为数字(01),但不能将日期从数字(01)转换为字符串(1月)?
答案 0 :(得分:2)
文档中解释了这一点:http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34924
字符串到日期转换规则
.....
.....
- 如果日期时间格式元素与日期字符串中相应字符之间的匹配失败,则Oracle会尝试其他格式元素,如表2-18所示。
**Table 2-18 Oracle Format Matching**
Original Format Element | Additional Format Elements to Try in Place of the Original
------------------------|-----------------------------------------------------------
'MM' | 'MON' and 'MONTH'
'MON' | 'MONTH'
'MONTH' | 'MON'
'YY' | 'YYYY'
'RR' | 'RRRR'
根据以上所述,下面列出的所有转化都可以正常运行:
alter session set NLS_LANGUAGE='American';
select to_date('21-jan-2014', 'dd-month-yyyy' ) from dual;
select to_date('21-january-2014', 'dd-mon-yyyy' ) from dual;
select to_date('21-january-2014', 'dd-mm-rrrr' ) from dual;
select to_date('21-jan-2014', 'dd-mm-rrrr' ) from dual;
select to_date('21-01-2014', 'dd-mm-yy' ) from dual;
select to_date('21-01-14', 'dd-mm-yyyy' ) from dual;
select to_date('21-01-2014', 'dd-mm-rr' ) from dual;
select to_date('21-01-14', 'dd-mm-rrrr' ) from dual;
alter session set NLS_LANGUAGE='Polish';
select to_date('21-sty-2014', 'dd-month-yyyy' ) from dual;
select to_date('21-styczeń-2014', 'dd-mon-yyyy' ) from dual;
select to_date('21-styczeń-2014', 'dd-mm-rrrr' ) from dual;
select to_date('21-sty-2014', 'dd-mm-rrrr' ) from dual;