[英]convert varchar[ ] column type to uuid[ ]
there is a column of type character varying[], which contains data of type uuid like {0f071799-e37e-4e1c-9620-e580447416fe,913a7134-6092-45fa-ae18-163302db8112}
,有一列字符类型不同[],其中包含 uuid 类型的数据,例如{0f071799-e37e-4e1c-9620-e580447416fe,913a7134-6092-45fa-ae18-163302db8112}
,
but there are also some old values of another type like {8f5f7cc46821423fa6057025a}
.但也有一些其他类型的旧值,例如{8f5f7cc46821423fa6057025a}
。
How can the column type be converted?列类型如何转换? I used the script:我使用了脚本:
alter table services alter column office_ids type uuid[] USING office_ids::uuid[];
but gives an error - invalid syntax for type uuid: "8f5f7cc46821423fa6057025a".但给出错误 - uuid 类型的语法无效:“8f5f7cc46821423fa6057025a”。
You must first convert your 25 character values into valid uuid
values.您必须首先将 25 个字符值转换为有效的uuid
值。
One such conversion would be:一种这样的转换是:
8f5f7cc46821423fa6057025a -> 00000008-f5f7-cc46-8214-23fa6057025a
The SQL for this is: SQL 是:
regexp_replace('8f5f7cc46821423fa6057025a', '^(.)(.{4})(.{4})(.{4})(.{12})^', '0000000\1-\2-\3-\4-\5')
output: output:
00000008-f5f7-cc46-8214-23fa6057025a
Which leaves valid uuids unchanged.这使有效的 uuid 保持不变。 See live demo .见现场演示。
You can use this to update the bad values like this:您可以使用它来更新错误的值,如下所示:
update services set office_ids = array(
select regexp_replace(t.val, '^(.)(.{4})(.{4})(.{4})(.{12})$', '0000000\1-\2-\3-\4-\5')
from unnest(services.office_ids) as t(val)
)
Then your alter command will work.然后您的 alter 命令将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.