Re: Cast char to number

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: jd(at)commandprompt(dot)com
Cc: Richard Huxton <dev(at)archonet(dot)com>, rod(at)iol(dot)ie, Christine Penner <christine(at)ingenioussoftware(dot)com>, 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 20:43:03
Message-ID: 29174.1267044183@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"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.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Scott Marlowe 2010-02-24 20:46:37 Re: Cast char to number
Previous Message Joshua D. Drake 2010-02-24 20:38:22 Re: Cast char to number