Re: How to change data type in column ?

From: "scott(dot)marlowe" <scott(dot)marlowe(at)ihs(dot)com>
To: Vitali Djatsuk <Vitali(dot)Djatsuk(at)MicroLink(dot)ee>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: How to change data type in column ?
Date: 2003-04-15 16:31:01
Message-ID: Pine.LNX.4.33.0304151027230.18680-100000@css120.ihs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, 15 Apr 2003, Vitali Djatsuk wrote:

>
> Hi,
>
> How can I change a data type in column ?

There are a lot of issues involved in changing the type. I.e. what
should the database do to "coerce" the data from one type to another
that's already in the table. should the misformed data in the input be
simply dropped and a null inserted, what if it's not null and doesn't fit,
do we toss the row? not convert?
The best way to get around these problems is to select the fields into a
new table and coerce the type as you do it. That way, you can see where
you're going before you get there.

create table test (name text, id int, balance numeric (12,2), num float);
insert some data...
select name, id, balance::float, num::numeric(12,2) into newtable;

Now you can check newtable, and if it's right you can then delete the old
table and rename newtable in its place.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Kurt Roeckx 2003-04-15 16:36:17 Re: Are we losing momentum?
Previous Message Richard Huxton 2003-04-15 16:29:45 Re: [PERFORM] Yet Another (Simple) Case of Index not used