-2

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?

klin
  • 112,967
  • 15
  • 204
  • 232

2 Answers2

4

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.

klin
  • 112,967
  • 15
  • 204
  • 232
0

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.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786