Sql 更新复合列
我只想用其他列的值填充_表的第7列,因此第7列值的结果如下:86 | WWB | 2014或86 | wb |-如果第3列的值为0。以下是我的SQL:Sql 更新复合列,sql,postgresql,Sql,Postgresql,我只想用其他列的值填充_表的第7列,因此第7列值的结果如下:86 | WWB | 2014或86 | wb |-如果第3列的值为0。以下是我的SQL: UPDATE public.my_table SET column7 = case when column1 IS NULL then '-' else column1 end || '|' || case when column2 IS NULL then '-' else column2 end || '|' || c
UPDATE public.my_table
SET column7 =
case when column1 IS NULL then '-' else column1 end
|| '|' ||
case when column2 IS NULL then '-' else column2 end
|| '|' ||
case when column3 = '0' then '-' else column3 end
error: invalid input syntax for integer: "_"
问题出现在最后一行,因为第3列是整数。
Column1和column2是bpchar,column3是专栏作家int2。
有办法解决这个问题吗?您遇到了类型冲突。在Postgres中很容易转换:
UPDATE public.my_table
SET column7 = (coalesce(column1::text, '-') || '|' ||
coalesce(column2::text, '-') || '|' ||
(case when column3 = 0 then '-' else column3::text end)
);
您正在发生类型冲突。在Postgres中很容易转换:
UPDATE public.my_table
SET column7 = (coalesce(column1::text, '-') || '|' ||
coalesce(column2::text, '-') || '|' ||
(case when column3 = 0 then '-' else column3::text end)
);
使用
concat
将使其更易于阅读,并自动将所有内容转换为文本。但是,case语句需要为所有分支生成相同的数据类型,因此对于column3
UPDATE public.my_table
SET column7 = concat(
coalesce(column1, '-'), '|'
coalesce(column2, '-'), '|'
case when coalesce(column3,0) = 0 then '-' else column3::text end
);
使用
concat
将使其更易于阅读,并自动将所有内容转换为文本。但是,case语句需要为所有分支生成相同的数据类型,因此对于column3
UPDATE public.my_table
SET column7 = concat(
coalesce(column1, '-'), '|'
coalesce(column2, '-'), '|'
case when coalesce(column3,0) = 0 then '-' else column3::text end
);
谢谢整数的问题解决了。对于column1和column2的列值[NULL],“-”列表中添加了“-”,例如“-|-| 2014,但当列值为“”时,结果如下所示:| | 2014。我想对于合并运算符,“”是一个“正常值”,谢谢!,整数的问题解决了。对于column1和column2的列值[NULL],“-”列表中添加了“-”,例如“-|-| 2014,但当列值为“”时,结果如下所示:| | 2014。我想对于合并运算符,“”是一个“正常值”,谢谢!,整数的问题解决了。对于column1和column2的列值[NULL],“-”列表中添加了“-”,例如“-|-| 2014,但当列值为“”时,结果如下所示:| | 2014。我假设对于合并运算符,“”是一个“正常值”@user615993。空字符串与
NULL
值不同。好的,谢谢,那么我应该使用“case when column1=''或column1为NULL然后“-”else column1 end”代替coalesce,如果我想同时捕获NULL和“”,我会suppose@user615993 . . . 是的,这可能就是你想要的逻辑。谢谢!,整数的问题解决了。对于column1和column2的列值[NULL],“-”列表中添加了“-”,例如“-|-| 2014,但当列值为“”时,结果如下所示:| | 2014。我假设对于合并运算符,“”是一个“正常值”@user615993。空字符串与NULL
值不同。好的,谢谢,那么我应该使用“case when column1=''或column1为NULL然后“-”else column1 end”代替coalesce,如果我想同时捕获NULL和“”,我会suppose@user615993 . . . 是的,这可能就是你想要的逻辑。