change column datatype from array to integer

我的梦境 提交于 2019-12-14 04:13:11

问题


I need to change the data type of a column from _float8 to int4

ALTER TABLE "table" ALTER COLUMN "col" SET DATA TYPE int4;

results in column "col" cannot be cast automatically to type integer

ALTER TABLE "table" ALTER COLUMN "col" SET DATA TYPE int4 USING (col::integer);

results in cannot cast type double precision[] to integer

any ideas?


回答1:


You have to point out which element of the array should be used in the conversion, e.g.

alter table x alter column y set data type int4 using (y[1]::int)

db<>fiddle.




回答2:


The problem is that you have an array. To fix this, you need array operators:

ALTER TABLE "table"
    ALTER COLUMN "col" SET DATA TYPE int4 USING (col[1]::integer);

Here is a db<>fiddle.



来源:https://stackoverflow.com/questions/56753071/change-column-datatype-from-array-to-integer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!