问题
I have the following SQL query which is looking for duplicate values in a table (two values being null must be classed as a duplicate, hence the use of nvl):
select * from PersonLinkTable personLink
where personLink.RefPerson = 100
and nvl(to_char(personLink.PersonLinkType), '###') = nvl(to_char(PersonLinkTable.PersonLinkType), '###') // Repeats for required columns
The third line repeats for the required columns, and is generated automatically in case any new columns are added in the future. The problem is that when I added to_char, this was tested and caused an "Invalid Number" Oracle error. I believe to_char is needed in case any date columns are added in the future.
Why would using to_char in this way cause an "Invalid Number" error, and what is the correct way to use it? The column types at the moment are varchar2, char and number.
This query works correctly on SQL Server with some changes - no to_char, isnull instead of nvl and the null values are changed to empty strings instead of '###'. This is part of a larger query, and has "and not exists" around it.
回答1:
To_Char expects to receive a number as its argument by default. If it receives a character string then it will try to implictly convert the string to a number, and that will raise this error message if the string cannot be converted.
If the column is of a character type, do not try to convert it to a string.
来源:https://stackoverflow.com/questions/31356854/oracle-invalid-number-caused-by-to-char-function