Skip to content
Advertisement

Invalid number when converting a date to another format

I have this query, and i am trying to format all dates for the two columns mentioned in the query below to the format mentioned:

and i want to convert the results from timeone and timetwo to another date format DD-MM-YYYY HH:MIPM but when I run the query I got back invalid number. The datatype of the timeone and timetwo columns in my table are date

When i run the query without the formatting, the results are:

enter image description here

Where did i go wrong?. All alternatives and solutions are welcomed

Advertisement

Answer

The datatype of the timeone and timetwo columns in my table are date

The error says that isn’t the case; they seem to be strings. As a date this would work:

but as a string it would get the error you see:

So, you are really trying to convert one string representation of a date to a differently-formatted string representation. To do that you need the inner function call to be to_date(), and the outer function call to be to_char() – you have yours the wrong way around:

The format model for the to-date() call also has to match the actual string value, so I’ve changed that to 'YYYY-MM-DD HH24:MI:SS'. And '28-MAY-2021' is also a string not a date, so – assuming that really is a date field – I’ve changed that to a date literal. You could also use a to_date() on that string value, but it’s easier with a literal (and if you supply a month name then you should specify the date language too).

db<>fiddle.

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