despite of using to_date function
Actually it's because of using the to_date()
function. You have things like this:
TO_DATE(SYSDATE,'DD-MM-YYYY')
TO_DATE(s2.end_time,'DD-MM-YYYY')
TO_DATE(next_day(SYSDATE,'SATURDAY'),'DD-MM-YYYY')
TO_DATE(next_day(SYSDATE,'SATURDAY')-(INTERVAL '7' DAY + INTERVAL '1' SECOND),'DD-MM-YYYY')
The second of those might be OK, but only if sd.end_time
is stored as a string - which it almost certainly shouldn't be.
For the other three, at least, you're passing something that is already a date into a function that is there to convert from a string to a date. So you're really doing, for example:
TO_DATE(TO_CHAR(SYSDATE),'DD-MM-YYYY')
and as that implicit TO_CHAR()
doesn't have a format model it will use your session's NLS settings, specifically NLS_DATE_FORMAT
. You're seeing it work in one environment because the settings there effectively mean it's doing:
TO_DATE(TO_CHAR(SYSDATE, 'DD-MM-YYYY'),'DD-MM-YYYY')
whereas in another session it might effectively be doing:
TO_DATE(TO_CHAR(SYSDATE, 'DD-MON-RR'),'DD-MM-YYYY')
... which would lose the century, leaving a date value in year 0019 instead of 2019; or
TO_DATE(TO_CHAR(SYSDATE, 'YYYY-MM-DD'),'DD-MM-YYYY')
which throws "ORA-01861: literal does not match format string" as you are seeing.
While you could try to force the NLS settings on every client and application to match what you're doing it'll go wrong eventually, and you should be doing that conversion at all. Just use SYSDATE
and other calculated directly instead; or if you're trying to get the dates with the times set to midnight truncate them:
trunc(SYSDATE)
trunc(s2.end_time)
trunc(next_day(SYSDATE,'SATURDAY'))
trunc(next_day(SYSDATE,'SATURDAY')-(INTERVAL '7' DAY + INTERVAL '1' SECOND))
Also note that the second argument to next_day()
is the day name or abbreviation in the current session date language, so someone running this from a session in a different language will also see errors (like "ORA-01846: not a valid day of the week").