Re: Cast char to number

Поиск
Список
Период
Сортировка
От Scott Marlowe
Тема Re: Cast char to number
Дата
Msg-id dcc563d11002241246x34979903j49054b5f43e3f833@mail.gmail.com
обсуждение исходный текст
Ответ на Re: Cast char to number  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-general
On Wed, Feb 24, 2010 at 1:43 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> "Joshua D. Drake" <jd@commandprompt.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.

В списке pgsql-general по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: Cast char to number
Следующее
От: Tom Lane
Дата:
Сообщение: Re: Curious plperl behavior