Re: Cast char to number

From: Christine Penner <christine(at)ingenioussoftware(dot)com>
To: Scott Marlowe <scott(dot)marlowe(at)gmail(dot)com>,Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: jd(at)commandprompt(dot)com,Richard Huxton <dev(at)archonet(dot)com>,rod(at)iol(dot)ie, Bill Moran <wmoran(at)potentialtech(dot)com>, Postgres-General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Cast char to number
Date: 2010-02-24 21:42:29
Message-ID: 19740413173256.28F0A2D0E7D13094@edtnaa03.telusplanet.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

This is what I did.

set all blank columns to '0' because they were causing errors.
alter table T alter column a type integer using a::integer

That worked perfectly.

Thanks everyone for the help.

Christine

At 12:46 PM 24/02/2010, Scott Marlowe wrote:
>On Wed, Feb 24, 2010 at 1:43 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> > "Joshua D. Drake" <jd(at)commandprompt(dot)com> writes:
> >> On Wed, 2010-02-24 at 20:22 +0000, Richard Huxton wrote:
> >>> ALTER TABLE t ALTER COLUMN c TYPE integer USING c::integer;
> >
> >> That won't work in this case. char() can't be cast to int/numeric. Not
> >> only that it isn't possible to clean up the data in table because char
> >> automatically pads.
> >
> >> postgres=# alter table foo alter column id type numeric;
> >> ERROR: column "id" cannot be cast to type "pg_catalog.numeric"
> >
> > That just indicates that there isn't an *implicit* coercion from char to
> > numeric. With a USING clause you can specify an arbitrary conversion.
> >
> > I agree with the recommendation to test it out before actually doing
> > the table change though. Maybe look at the results of
> >
> > select id, id::numeric from your_table
> >
> > to see if it looks sane for all the different data formats in the
> > column.
>
>And if the database is mostly sitting idle (i.e. no other users) you
>can always just do
>
>begin;
>alter table yada;
>
>test how it went and then commit or rollback.
>
>For the OP: It's a bad idea to do that kind of stuff in production
>cause you'll put a lock on the table others will have to wait for.
>
>--
>Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
>To make changes to your subscription:
>http://www.postgresql.org/mailpref/pgsql-general

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Raymond O'Donnell 2010-02-24 21:47:00 Re: Cast char to number
Previous Message Tom Lane 2010-02-24 21:34:11 Re: Curious plperl behavior