PostgreSQL COALESCE function

The COALESCE function returns the first non-null value in the parameters. It requires at least one of the parameters to be non-null. If the parameters are all null, an error will be reported.

select COALESCE(null,null); //报错
select COALESCE(null,null,now(),''); //结果会得到当前的时间
select COALESCE(null,null,'',now()); //结果会得到''

//可以和其他函数配合来实现一些复杂点的功能:查询学生姓名,如果学生名字为null或''则显示“姓名为空”
select case when coalesce(name,'') = '' then '姓名为空' else name end from student;
 

 

Guess you like

Origin blog.csdn.net/londa/article/details/108719162