Skip to content
Advertisement

concatenate with leading zeros for months – not a valid month error

I want to concatenate string with year in oracle. The string is month. Query is

This query wrks fine however if the string is ’01’ or anything starting with zero i am getting not a valid month error.

Advertisement

Answer

Let’s run it without the to_date(..., ...) wrapper:

Do you see the problem? (Note that the result is a number, not even a string.)

Concatenation is performed before you subtract 1. You get the string ‘042020’ first, and then you subtract 1; the string is converted to number, and you get the result 42019. When you apply TO_DATE() this is converted back to string, and the first thing Oracle will choke on is the 42 for month.

Use parentheses around the arithmetic operation to force the subtraction to be performed first, and it will work:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement