if(condition, then, else) in Oracle

╄→гoц情女王★ 提交于 2019-12-04 00:58:12

Use the standard COALESCE function:

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

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

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.

You want to use NVL, or NVL2

NVL(t.column, 0)
NVL2( string1, value_if_NOT_null, value_if_null )

Yup, NVL should do the trick.

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

Oracle does support various ifs and cases as well.

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

(select nvl(foo.bar,0)... 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!