Re: Reg: Changing Column type

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: sandhya <sandhyar(at)amiindia(dot)co(dot)in>
Cc: postgre <pgsql-admin(at)postgresql(dot)org>
Subject: Re: Reg: Changing Column type
Date: 2005-11-16 19:35:24
Message-ID: 20051116193524.GA4302@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

On Tue, Nov 15, 2005 at 17:22:16 +0530,
sandhya <sandhyar(at)amiindia(dot)co(dot)in> wrote:
> Hi,
>
> When i am tring to change the column type from character/Varchar to "int" i am getting error like,
>
> ERROR: column "fname" cannot be cast to type "int4"
>
> I can able to change the column type to "int " if the old datatype is double etc.
> Even a "int" datatype also i can able to conver to "char"
> and why not viceversa....Please tell me where the problem is.
> Suggest me what to do to convert a "char" datatype to "int".

One way to do this is to add a new int column. Use a query to insert data into
the new column using to_number allow with whatever you need to handle strings
that aren't numbers.
Then delete the old column.
Then rename the new column to be the original name.

In at least 8.1 there is a USING clause which allows you to specify a
conversion function which lets you do the above in one step.
I don't know when that feature was added though.

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Flávio Brito 2005-11-16 20:21:31 Receive a record not a tuple - plpgsql
Previous Message Tom Lane 2005-11-16 18:58:13 Re: ERROR: could not read block