如何将一列逗号分隔的字符串转换为字符串数组

问题描述 投票:0回答:2

我在PostgreSQL中有此表,其中的一列是包含称为directors的nconst的字符串。字符串的格式为nm00386378,nm97284993,我需要将每个字符串转换为数组,例如:{"nm00386378","nm97284993"}

我尝试了以下代码,但我认为它只是将字符串转换为一个值数组:

alter table crew_text
    alter directors drop default,
    alter directors type text[] using array[directors],
    alter directors set default '{}';

我获得了{"nm00386378,nm97284993"}作为数组值,但我期望是{"nm00386378","nm97284993"}

arrays postgresql ddl
2个回答
1
投票

尝试为USING使用以下ALTER TABLE ... ALTER ... TYPE子句:

USING regexp_split_to_array(directors, ',')

0
投票

像这样使用string_to_array()

string_to_array()

db <>小提琴alter table crew_text alter directors drop default , alter directors type text[] using string_to_array(directors, ',') , alter directors set default '{}';

仅在需要时使用正则表达式函数。这些功能更强大,但也更棘手,而且价格更高。

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