query is :
select substr(to_date('01-02-2018','mm-dd-yyyy'),4,3) from dual;
output is :
JAN
can anyone explain how the output came ?
query is :
select substr(to_date('01-02-2018','mm-dd-yyyy'),4,3) from dual;
output is :
JAN
can anyone explain how the output came ?
When you apply a text function directly to something that's of DATE datatype, you force an implicit conversion of the date into a string. This conversion uses the NLS_DATE_FORMAT parameter to decide the format of the output string.
In effect,
substr(to_date('01-02-2018','mm-dd-yyyy'),4,3)
is the same as
substr(to_char(to_date('01-02-2018','mm-dd-yyyy'), <NLS_DATE_FORMAT>),4,3)
The usual default value (for English-language versions of the database) of the NLS_DATE_FORMAT parameter is DD-MON-RR
- which it sounds like the value of your NLS_DATE_FORMAT parameter is set to, which means your query is doing:
substr(to_char(to_date('01-02-2018','mm-dd-yyyy'), 'DD-MON-RR'),4,3)
Therefore, the substr
is working on the string 02-JAN-18
, and the 3 characters starting from the 4th character is JAN
.
Rather than use substr
on a date, you would do better to use to_char
instead, e.g.:
to_char(to_date('01-02-2018', 'mm-dd-yyyy'), 'MON')