PL SQL : NVL first parameter type conversion issue

15.4k Views Asked by At

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

5

There are 5 best solutions below

0
Lost in Alabama On BEST ANSWER

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.

0
pablo On

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

0
Joe Stefanelli On

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.

0
Erkan Haspulat On

"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.

0
Pavan Ebbadi On

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