if(condition, then, else) in Oracle

后端 未结 6 2724
甜味超标
甜味超标 2021-02-20 01:15

MySQL/MSSQL has a neat little inline if function you can use within queries to detect null values, as shown below.

SELECT

...

foo.a_field AS \"a_field\",
SELEC         


        
相关标签:
6条回答
  • 2021-02-20 01:43

    Just do nvl(foo.bar,0), you don't have to do

    (select nvl(foo.bar,0)... 
    
    0 讨论(0)
  • 2021-02-20 01:48

    Use the standard COALESCE function:

    SELECT COALESCE(foo.bar, 0) as "bar", ...
    

    Or use Oracle's own NVL function that does the same.

    0 讨论(0)
  • 2021-02-20 01:48

    Yup, NVL should do the trick.

    0 讨论(0)
  • 2021-02-20 01:59

    You want the nvl function: nvl(foo.bar, 0)

    Oracle does support various ifs and cases as well.

    0 讨论(0)
  • 2021-02-20 02:01

    To supplement the rest of the answers here, which deal primarily with NULL values and COALESCE/NVL/NVL2:

    SELECT *
    FROM TheTable
    WHERE field1 = CASE field2 WHEN 0 THEN 'abc' WHEN 1 THEN 'def' ELSE '' END
    

    CASE statements are not as succinct, obviously, but they are geared towards flexibility. This is particularly useful when your conditions are not based on NULL-ness.

    0 讨论(0)
  • 2021-02-20 02:02

    You want to use NVL, or NVL2

    NVL(t.column, 0)
    NVL2( string1, value_if_NOT_null, value_if_null )
    
    0 讨论(0)
提交回复
热议问题