NULLIF函数:如果提供的表达式相等,nullif 函数返回一个空值。
否则,它将返回第一个表达式作为结果。
--nullif function returns a null value if provided expressions are equal.
--If two expressions provided are equal, then it provides a null value;
--otherwise, it will return the first expression as a result.
select nullif(50,50);
--输出结果
NULL
select nullif('','');
select nullif(null,null);
--输出结果
NULL
select nullif('50', null)
--输出结果
50
select nullif(null, '50')
--输出结果
NULL
select nullif(null, '')
--输出结果
NULL
select nullif('', null)
--输出结果
空字符串
coalesce函数:返回第一个非空值
--COALESCE (Argument1, Argument2,….,ArgumentN);
--COALESCE returns as first non-null value as a result
--coalesce返回第一个非空值
select coalesce(null, 50);
select coalesce(50, null);
--返回结果均为50
select coalesce(null,null);
--返回NULL
select coalesce('','');
--返回空字符串
select coalesce (NULL, 20, 30, 40, 50);
--返回20
select coalesce(null, '')
--返回空字符串
一般将两者结合起来使用
--如果columnA字段为NULL或者是空字符串'',使用nullif函数首先将其与空字符串'',得到NULL
--因为如果ColumnA为NULL, 则nullif(NULL,'')为NULL
--因为如果ColumnA为'', 则nullif('','')为NULL
--结合coalesce函数取第一个非空值,则可以得到字符串'0'
select coalesce(nullif(columnA, ''), '0')
参考链接:/postgresql-nullif/