Oracle “Invalid Number” caused by to_char function

一世执手 提交于 2019-12-04 06:33:19

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!