如何匹配postgres中的regexp_replace函数中的空值

时间:2022-02-21 11:46:16

I want to replace null values into some string when I retrieve the data from my tables. I used the regexp_replace() function but I don't know how to match null pattern.

我想从表中检索数据时将空值替换为某些字符串。我使用了regexp_replace()函数,但我不知道如何匹配null模式。

I tried following queries:

我试过以下查询:

select regexp_replace(name,NULL,'false') from student;
select regexp_replace(name,'\0','false') from student;

but it is not giving the expected answer.

但它没有给出预期的答案。

1 个解决方案

#1


When you want to substitute NULL values for some other value in a query, you can use the coalesce() function:

如果要将NULL值替换为查询中的某些其他值,可以使用coalesce()函数:

SELECT coalesce(name, 'false') FROM student;

You can have any number of expressions as parameters in the coalesce() function. The expressions are evaluated from left to right until a non-NULL value is found, which is returned.

您可以在coalesce()函数中将任意数量的表达式作为参数。表达式从左到右进行计算,直到找到非NULL值,并返回该值。

#1


When you want to substitute NULL values for some other value in a query, you can use the coalesce() function:

如果要将NULL值替换为查询中的某些其他值,可以使用coalesce()函数:

SELECT coalesce(name, 'false') FROM student;

You can have any number of expressions as parameters in the coalesce() function. The expressions are evaluated from left to right until a non-NULL value is found, which is returned.

您可以在coalesce()函数中将任意数量的表达式作为参数。表达式从左到右进行计算,直到找到非NULL值,并返回该值。