Re: Cast char to number

From: Richard Huxton <dev(at)archonet(dot)com>
To: rod(at)iol(dot)ie
Cc: 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:22:07
Message-ID: 4B858A6F.3070206@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 24/02/10 20:06, Raymond O'Donnell wrote:
> However, to address your immediate problem, you could try something like
> this:
>
> (i) Create a new column of type numeric or integer as appropriate.
> (ii) update your_table set new_column = CAST(trim(both ' 0' from
> old_column) as numeric)
> (iii) Drop the old column, as well as any constraints depending on it.

Or, in any recent version of PG you can do this via ALTER TABLE
http://www.postgresql.org/docs/8.4/static/sql-altertable.html

ALTER TABLE t ALTER COLUMN c TYPE integer USING c::integer;

You might want to clean up the values before doing this.

--
Richard Huxton
Archonet Ltd

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Daniel Verite 2010-02-24 20:25:35 Re: Cast char to number
Previous Message Raymond O'Donnell 2010-02-24 20:06:50 Re: Cast char to number