6

I have a table Application which has a column

BORROWINGTERM   NUMBER(10,0) Nullable

why this script throw an error (ORA-01722 invalid number)

select nvl(borrowingterm, 'no term') from Application 

while this one works

select nvl(to_char(borrowingterm), 'no term') from Application 

and this one also works

select nvl(1234,5678) from dual; 

base on this article

the first parameter of NVL function should be string type

4

5 に答える 5

9

You're getting the error because you are mixing two different types in the nvl clause. One number, one string. The types must be the same.

于 2011-03-01T14:42:03.930 に答える
6

Keeping it simple ,

  • String null can be replaced with string Substitute value.
  • Number null can be replaced by number value and so on..

Example:

select nvl(to_char(borrowingterm), 'no term') from Application; 

//conert number to string and then provide string substitute value to column

select nvl(borrowingterm, 0') from Application; 

// replacing null with 0

于 2016-04-07T14:56:28.360 に答える
5

In short, if the first argument is numeric then Oracle attempts to convert the second argument to a number.

See NVL documentation

http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions105.htm#i91798

于 2011-03-01T14:44:22.837 に答える
5

In your first example, because borrowingterm is a numeric type, Oracle is trying to implicitly convert the string 'no term' to a numeric, hence the error.

See the NVL documentation.

于 2011-03-01T14:46:59.293 に答える
2

"the first parameter of NVL function should be string type"

I think you gave the answer yourself. If you are asking why, you should consider the possibility of reusing the output type of NVL in another function.

select to_date(nvl(number_field, 'some_text')) from dual

In the situation above, the output of nvl is unknown, thus an ORA-01722 is raised.

于 2011-03-01T14:47:46.867 に答える