SQL中的TRIM函数可以更精确吗?

问题描述 投票:1回答:1

我有一个导入表,我想拆分成其他表。导入表中有一列,其字段的数据具有以下格式:['email', 'phone', 'facebook']

为了将字段中的每个值(在这种情况下为电子邮件,电话和facebook)分开,当我将其插入对应的表时,我正在使用函数TRIM,如下所示:

insert into Media (media)
select distinct trim ('{}' from regexp_split_to_table(host_media, ','))
from ImportH;

但是插入到新表中的数据看起来很脏,例如在第1行中,我将有['email',在第2行中,'phone'和在第3行中:'facebook']

如何使数据以干净的方式插入表中,而没有'['和浮点逗号?

我将提供此列的导入表数据的图像以及拆分后得到的图像:

enter image description here

enter image description here

sql postgresql trim
1个回答
0
投票

您可以只更改分离器:

select *
from regexp_split_to_table('[''email'', ''phone'', ''facebook'']', '[^_a-zA-Z]+') s(value)
where value <> '';

分隔符就是您想要的字符串中不是有效字符的任何字符。

Here是db <>小提琴。


0
投票

其中一种选择...也许对您没问题:

with trimed as
(select substring(host_media from 2 for length(host_media)-2) as clean_col
from ImportH
)
insert into Media (media)
select unnest(string_to_array(clean_col, ',')) 
from trimed;

Here is a demo

© www.soinside.com 2019 - 2024. All rights reserved.