Home > Archive > PostgreSQL Discussion > December 2005 > alter column datatype with cast









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author alter column datatype with cast
Klein Balázs

2005-12-29, 8:23 pm

I had to change the datatype of a column from text to integer. The column
contained integers (obviously stored as text).

When I tried to change the datatype of the column I got an error message
saying that the column can not be cast to integer:

Operation : ALTER TABLE "public"." subjectgroupconditio
n" ALTER COLUMN
"param1" TYPE INTEGER
Result : "ERROR: column "param1" cannot be cast to type
"pg_catalog.int4""

However when I created an other integer column in the table and updated it
from the text column there was no problem casting the data:
Operation : UPDATE public. subjectgroupconditio
n SET param2 = cast(param1 as
integer);
Result : "OK."

Since pg knows that it should cast the data and it can cast it I think I
should have been able to change the datatype in the first instance. Maybe
this behaviour has a good reason but I don't know what it is.

Regards,
SWK


---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Michael Fuhr

2005-12-29, 8:23 pm

On Thu, Dec 29, 2005 at 09:46:10PM +0100, Klein Balzs wrote:
> I had to change the datatype of a column from text to integer. The column
> contained integers (obviously stored as text).
>
> When I tried to change the datatype of the column I got an error message
> saying that the column can not be cast to integer:
>
> Operation : ALTER TABLE "public"." subjectgroupconditio
n" ALTER COLUMN
> "param1" TYPE INTEGER
> Result : "ERROR: column "param1" cannot be cast to type
> "pg_catalog.int4""


Use the USING clause:

ALTER TABLE subjectgroupconditio
n
ALTER COLUMN param1 TYPE integer USING param1::integer;

> However when I created an other integer column in the table and updated it
> from the text column there was no problem casting the data:
> Operation : UPDATE public. subjectgroupconditio
n SET param2 = cast(param1 as
> integer);
> Result : "OK."
>
> Since pg knows that it should cast the data and it can cast it I think I
> should have been able to change the datatype in the first instance. Maybe
> this behaviour has a good reason but I don't know what it is.


Some casts can be done implicitly and some not. For more information
see the CREATE CAST and Type Conversion documentation:

http://www.postgresql.org/docs/8.1/...createcast.html
http://www.postgresql.org/docs/8.1/...e/typeconv.html

--
Michael Fuhr

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql
.org so that your
message can get through to the mailing list cleanly

Sponsored Links





Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive | Programming forum archive

Copyright 2008 droptable.com