我在一个表中有一个名为“directedlink_href”的字段(类型为字符变化),其中包含的数组的值都以“#”字符开头。
如何从该字段中这些数组的任何条目中删除“#”字符?
例如...
{#osgb4000000030451486,#osgb4000000030451491}
到
{osgb4000000030451486,osgb4000000030451491}
最佳答案
干净的解决方案是取消嵌套、替换然后重新聚合值:
select id,
(select array_agg(substr(x.val,2) order by x.idx) from unnest(t1.directedlink_href) with ordinality as x(val,idx)) as data
from the_table t1;
如果你真的想改变表中的数据:
update the_table t1
set directedlink_href = (select array_agg(substr(x.val,2) order by x.idx) from unnest(t1.directedlink_href) with ordinality as x(val,idx));
这只是去掉了第一个字符。如果值的开头可能有其他字符,则需要使用 regexp_replace(x.val,'^#', '')
而不是 substr(x.val, 2)
关于sql - 从 PostgreSQL 表中的数组中删除 # 个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40469491/