Re: Changing datatype of a column

From: Jason Earl <jdearl(at)yahoo(dot)com>
To: Ilan Fait <ilan(at)iweb(dot)com>, "'Pgsql-Sql \(E-mail\)" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Changing datatype of a column
Date: 2001-06-26 16:28:22
Message-ID: 20010626162822.90915.qmail@web10001.mail.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

There is no way to do this directly, but it is a
pretty simple task to do this using SELECT INTO.

Here is a trivial example of how to do this.

First of all let's say you have a table that looks
like this with some values in it:

CREATE TABLE foo (
num int
);

processdata=> select * from foo;
num
-----
1
2
3
(3 rows)

A simple query using SELECT INTO would give you the
table that you need.

processdata=> select num::numeric(11) as num into bar
from foo;

Here's the results.

processdata=> \d bar
Table "bar"
Attribute | Type | Modifier
-----------+---------------+----------
num | numeric(11,0) |

processdata=> select * from bar;
num
-----
1
2
3
(3 rows)

You can then drop the original table and use alter
table to change the name of the new table. You can
also add constraints and whatever else you need.

Hope this is helpful,
Jason
--- Ilan Fait <ilan(at)iweb(dot)com> wrote:
>
> Hi,
>
> I would like to Know if there any way
> (and what is the command
> for that) to convert a column datatype
> from int4 to numeric(11) of an
> existing table that have data
> in it?
>
>
> Thanks,
> Ilan
>
>
> _________________________________________
> ILAN FAIT
> Tel: 972-9-9519133 Ex.247 iWeb Technologies
> Fax: 972-9-9519134 91 Medinat
> Ha'Yehudim St.
>
> Herzliya 46120 IL
> mailto:ilan(at)iweb(dot)com www.iweb.com
>
>
>

__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail
http://personal.mail.yahoo.com/

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Kristis Makris 2001-06-26 19:58:51 Re: Using the extract() function in plpgsql
Previous Message Meggus 2001-06-26 15:34:25 simple function crashes my postmaster