将字符串数组更新为LOWER(小写)

时间:2018-06-29 08:14:08

标签: sql postgresql

如何更新包含字符串数组的列,以将每个字符串转换为小写字母?

类似于Update all values of a column to lowercase,但要包含字符串数组。

数据示例:

id | tags                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
---+---------------------------------------------------------------
58 | 
87 | {Pasta}
94 | {trendy,Supper,"Restaurant casual"}
...

2 个答案:

答案 0 :(得分:3)

您可以将数组转换为文本,在其上应用lower(),然后将其转换回数组:

update the_table
  set tags = lower(tags::text)::text[];

答案 1 :(得分:1)

也许您可以从数组中创建一个字符串,将其小写,然后一次性将其恢复为数组-尝试:

create table myTest (
id bigserial primary key,
arrayText text[]);

insert into myTest (arrayText) values ('{"aPPLE","GRAPE","piNEappLe","CHErry"}');
select * from myTest;
update myTest set arrayText = string_to_array(LOWER(array_to_string(arraytext,',')),',') where id = 1;
select * from myTest;`