我在 PostgreSQL 中有这个表,其中一列是一个包含 nconst 的字符串,称为 directors
。字符串的格式为 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"}
。
最佳答案
使用string_to_array()
像这样:
alter table crew_text
alter directors drop default
, alter directors type text[] using string_to_array(directors, ',')
, alter directors set default '{}';
db<> fiddle here
仅在需要时才使用正则表达式函数。这些功能更强大,但也更棘手,而且成本更高。
关于arrays - 如何将一列以逗号分隔的字符串转换为字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58122598/