Oracle – How to Fix ‘Literal Does Not Match Format String’ Error

date formatoracleoracle-12crestorestored-procedures

I' am having this error:

 ORA-01861: literal does not match format string
    01861. 00000 -  "literal does not match format string"
    *Cause:    Literals in the input must be the same length as literals in
               the format string (with the exception of leading whitespace).  If the
               "FX" modifier has been toggled on, the literal must match exactly,
               with no extra whitespace.
    *Action:   Correct the format string to match the literal.

It happened overnight without any changes in the database configuration. It looks like there is a problem with sysdate format but I'm not 100% sure. We have many places in our procedures where we use to_date(to_char(sysdate, 'YYYY-MM-DD')) from dual which actually breaks. Is there any way to format sysdate to match the following expression?

to_date(to_char(sysdate, 'YYYY-MM-DD')) from dual

Best Answer

First you convert sysdate to a char type, then you convert it back to a date type without specifying the format string, which may or may not work based on the settings of the session running this code.

This is completely unnecessary, just use trunc(sysdate) to remove the hours, minutes and seconds.

Also, if you return a date type, then the format depends on the nls_date_format variable, not the SQL that produces the date value.