我有以下内容:
Select Coalesce(Other,Industry) Ind from registration
问题是其他可以是空字符串或NULL。 如何使coalesce工作,如果Other是一个空字符串,Coalesce仍然应该按照它应该运行?
答案 0 :(得分:112)
使用CASE
表达式或NULLIF
:
SELECT COALESCE(NULLIF(Other,''),Industry) Ind FROM registration
答案 1 :(得分:15)
试试这个
Select Coalesce(nullif(Other,''),Industry) Ind from registration
答案 2 :(得分:3)
您也可以使用快捷方式,知道NULL <> ''
未评估为TRUE
...
CASE WHEN other <> '' THEN other ELSE industry END
然后逻辑如下......
CASE WHEN 'fubar' <> '' THEN other ELSE industry END
=&GT; CASE WHEN true THEN other ELSE industry END
=&GT; other
CASE WHEN '' <> '' THEN other ELSE industry END
=&GT; CASE WHEN false THEN other ELSE industry END
=&GT; industry
CASE WHEN NULL <> '' THEN other ELSE industry END
=&GT; CASE WHEN NULL THEN other ELSE industry END
=&GT; industry