如何在PostgreSQL中删除两个字符之间的字符串?

时间:2019-04-09 15:32:16

标签: sql postgresql

我有一列包含地址信息。我想删除#,之间的所有内容,但也要删除#并保留,

编辑:我还需要删除#前的空格。

这是我的专栏的样子:

ADDRESS
123 abc st. #123, city, zipcode
321 def road #321, city, zipcode

所以我的专栏看起来像这样:

ADDRESS
123 abc st., city, zipcode
321 def road, city, zipcode

2 个答案:

答案 0 :(得分:0)

根据您的示例,您还希望删除#前的空格:

select str, regexp_replace(str, ' #[^,]+,', ',')
from (values ('321 def road #321, city, zipcode')) v(str)

答案 1 :(得分:0)

您可以使用positive lookahead

select address, regexp_replace(address,' #[^,]+(?=,)','') from foo;
address                          | regexp_replace             
:------------------------------- | :--------------------------
123 abc st. #123, city, zipcode  | 123 abc st., city, zipcode 
321 def road #321, city, zipcode | 321 def road, city, zipcode

db <>提琴here

相关问题