Postgres,用 NULL 替换所有整数值

Postgres, replace all integer values with NULL

我正在尝试将值为 0 的列中的所有值替换为 NULL

SELECT score
REPLACE(score, 0, ISNULL)
FROM kpis
WHERE score=0

SELECT score
REPLACE(score, 0, ISNULL)
FROM kpis

但是我得到一个语法错误:

ERROR:  syntax error at or near "REPLACE"
LINE 2: REPLACE(score, 0, ISNULL)

将值为0的列中的所有值替换为NULL

update kpis set score = NULL where score = 0;

将列中的所有整数值替换为 NULL

update kpis set score = NULL where score ~ '^[0-9]*$'

用例

SELECT 
case when score = 0 then null else score end as score
from kpis 

您可以使用 NULLIF() 而不是替换。当 score 为 0 时,下面的查询将设置 score=null。

update kpis set score=NULLIF(score, 0)

或者您可以按照@Sas 的建议简单地使用下面的查询。

update kpis set score = NULL where score = 0;

您可以使用 nullif(),它在功能上等同于 CASE 表达式,但写起来更短一些。

SELECT nullif(score, 0) as score
FROM kpis