Why is Oracle's DECODE giving me a different value than NVL?

前端 未结 4 2004
旧巷少年郎
旧巷少年郎 2020-12-18 08:19

This query:

select nvl(0.75,0) from dual

gives me 0.75 (numeric) but this query:

select decode(1,0,null,0.75         


        
相关标签:
4条回答
  • 2020-12-18 08:49

    It's because the 3rd parameter of your decode statement is NULL; as per the documentation1 (my emphasis).

    Oracle automatically converts expr and each search value to the data type of the first search value before comparing.... If the first result has the data type CHAR or if the first result is null, then Oracle converts the return value to the data type VARCHAR2.

    In your case the first result is NULL, which Oracle treats as a VARCHAR2. Your return value is being implicitly converted to a VARCHAR2. If you changed your DECODE() to the following you'd get a number:

    select decode(1, 0, 0, 0.75)
    

    and you could achieve your NULL by using the NULLIF() function:

    select nullif(decode(1, 0, 0, 0.75), 0) ...
    

    It's better to use a CASE statement, which enforces that all returned datatypes are the same:

    select case 1 when 0 then null
                  else 0.75
           end ...
    

    1. which I've been caught out on as well.

    0 讨论(0)
  • 2020-12-18 08:52
    select to_number(decode(1,0,null,0.75)) from dual
    
    0 讨论(0)
  • 2020-12-18 09:08

    In the first case, nvl() is returning a numeric value. How to display that is up to the program you are using to run your queries. TOAD displays it like you said, 0.75.

    In the second example, decode() is returning a varchar2. When Oracle converts a number to a string without any formatting, this is what you get, i.e. ".75".

    From the Oracle docs on decode():

    If the first result has the datatype CHAR or if the first result is null, then Oracle converts the return value to the datatype VARCHAR2.

    You could use a number format and rtrim() to achieve your purpose, e.g.:

    select rtrim(to_char(.75, '9990.99999999999999'),'0') from dual;
    

    Result:

    0.75
    
    0 讨论(0)
  • 2020-12-18 09:11

    You can use

    select decode(1,0,to_number(null),0.75) from dual
    
    0 讨论(0)
提交回复
热议问题