The problem is that the DECODE
forces an implicit conversion. The return datatype of the DECODE
function is determined by the rule:
DECODE(expr, search, result [, search, result]* [, default])
Oracle automatically converts expr
and each search
value to the data type of the first search
value before comparing. Oracle automatically converts the return value to the same data type as the first result
. If the first result
has the data type CHAR or if the first result
is null, then Oracle converts the return value to the data type VARCHAR2
.
In Oracle NULL
has the default datatype of VARCHAR2
so your DECODE
expression is equivalent to:
DECODE(TO_NUMBER(src.retirement), 0,
NULL,
TO_CHAR(TO_DATE(src.retirement, 'YYYY.MM.DD')));
Without format, the TO_CHAR
uses your NLS_DATE_FORMAT
session setting, probably the default DD-MON-RR
which loses the century information.
As noted in this related question, the rules for RR
are as follow:
- If the specified two-digit year is 00 to 49, then
- If the last two digits of the current year are 00 to 49, then the returned year has the same first two digits as the current year.
- If the last two digits of the current year are 50 to 99, then the first 2 digits of the returned year are 1 greater than the first 2 digits of the current year.
- If the specified two-digit year is 50 to 99, then
- If the last two digits of the current year are 00 to 49, then the first 2 digits of the returned year are 1 less than the first 2 digits of the current year.
- If the last two digits of the current year are 50 to 99, then the returned year has the same first two digits as the current year.
This is why dates before 2050 had not triggered the mysterious behaviour!