From: Bruno Wolff III <bruno@wolff.to>
To: sandhya <sandhyar@amiindia.co.in>
Cc: postgre <pgsql-admin@postgresql.org>
Subject: Re: Reg: Changing Column type
Date: Wed, 16 Nov 2005 13:35:24 -0600
Message-ID: <20051116193524.GA4302@wolff.to> (raw)
In-Reply-To: <03e901c5e9db$06ba00d0$cd00000a@amiindiahjwbgh>
References: <03e901c5e9db$06ba00d0$cd00000a@amiindiahjwbgh>
On Tue, Nov 15, 2005 at 17:22:16 +0530,
sandhya <sandhyar@amiindia.co.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.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-admin@postgresql.org
Cc: bruno@wolff.to, sandhyar@amiindia.co.in
Subject: Re: Reg: Changing Column type
In-Reply-To: <20051116193524.GA4302@wolff.to>
References: <03e901c5e9db$06ba00d0$cd00000a@amiindiahjwbgh> <20051116193524.GA4302@wolff.to>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox