When you do
where on_date='23-aug-75'
It's actually translating that to:
where on_date=to_date('23-aug-75') -- without the fmt parameter.
Because no format is specified explicitly, the string gets parsed and converted to a date according to the current nls_date_format
session value. Most likely, your session value is DD-MON-RR
. You can check by querying:
select value
from nls_session_parameters
where parameter = 'NLS_DATE_FORMAT';
If that's the case, then the documentation on the The RR Datetime Format Element explains how the year is determined for the string 75
if the current date is 2015
:
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.
So, applying the above rule, when you are applying the condition:
where on_date='23-aug-75'
... then it's looking for the date of August 23rd, 1975.
In your question you also say:
I am asking this because if command #2 is not executed, then the result would definitely contain 23-aug-2075
.
I have to disagree with that statement. There is no way that your query will suddenly start matching to August 23rd, 2075, unless you change the nls_date_format
session value to something else like DD-MON-YY
.
So that's the explanation. But as for best practice, why not avoid the ambiguities altogether by always being explicit about the date format you are using and always using 4-digit year formats, or by using the ISO format YYYY-MM-DD
which is so clear and easy to use.