Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sql function - NANVL - different behaviour

Tags:

sql

oracle

Query1:

SQL> select nanvl(to_char(2),0) a from dual;

     A
    ---
     2

Query2:

SQL> select nanvl(to_char(2),null) a from dual;

     A
   -----

Doubt: Why the first query returns 2? I was expecting 0 as in query 2. I am confused . Please explain for me to understand.

like image 999
KrGk Avatar asked Aug 20 '14 04:08

KrGk


1 Answers

select nanvl(to_char(2),null) a from dual;

is equivalent to

select nanvl('2'),null) a from dual;

The literal value '2' is implicitly type converted into the floating point value 2, so nanvl() returns 2

try

select nanvl('NaN'),0) a from dual;

that will give you 0

like image 80
Tim Child Avatar answered Sep 20 '22 18:09

Tim Child