[英]Postgres: REGEXP_REPLACE using multiple columns
I have a table like this:我有一张这样的桌子:
Address | Remove1 | Remove2 | Remove3
-------------------------------------------------------------
100 Street, City, State Zip | Street | City | State
I want output like this:我想要这样的 output :
Address | Remove1 | Remove2 | Remove3 | OutputCol
------------------------------------------------------------------------------
100 Street, City, State Zip | Street | City | State | 100 *, *, * Zip
Basically I want to replace the text in Address Column matching on multiple remove columns with '*'基本上我想用'*'替换多个删除列上的地址列匹配中的文本
I have tried using REGEXP_REPLACE
and REPLACE
functions but both of them are giving partial results.我曾尝试使用
REGEXP_REPLACE
和REPLACE
函数,但它们都给出了部分结果。 Is there a way I can use the REGEXP_REPLACE(Address, (Remove1|Remove2|Remove3), '*', 'g')
?有没有办法可以使用
REGEXP_REPLACE(Address, (Remove1|Remove2|Remove3), '*', 'g')
?
I think that you are quite close.我认为你很接近。 You just need to properly concatenate the column values, like so:
您只需要正确连接列值,如下所示:
regexp_replace(
address,
concat_ws('|', remove1, remove2, remove3),
'*',
'g'
)
Demo on DB Fiddle : DB Fiddle 上的演示:
select
t.*,
regexp_replace(address, concat_ws('|', remove1, remove2, remove3), '*', 'g') OuputCol
from mytable t
address | remove1 | remove2 | remove3 | ouputcol :-------------------------- | :------ | :------ | :------ | :-------------- 100 Street, City, State Zip | Street | City | State | 100 *, *, * Zip
This is same as what you did.这和你做的一样。 You just need string concatenation
你只需要字符串连接
with wt as
(
select '100 Street, City, State Zip' address,
'Street' Remove1, 'City' Remove2, 'State' Remove3
)
select REGEXP_REPLACE(address, '(' || Remove1 || '|' || Remove2 ||'|'||Remove3 ||')' , '*', 'g') from wt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.