Re: Can't cast from char to integer...

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: Can't cast from char to integer...
Дата
Msg-id 25861.1222730803@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Can't cast from char to integer...  (Mike Diehl <mdiehl@diehlnet.com>)
Ответы Re: Can't cast from char to integer...  (Mike Diehl <mdiehl@diehlnet.com>)
Список pgsql-general
Mike Diehl <mdiehl@diehlnet.com> writes:
> For example, I'm trying to do this:

> insert into data
>     select cast('666' as integer) as block_number, phone as phone_number, name
> from demo_q;

> When I run this query, I get:

> ERROR:  column "block_number" is of type integer but expression is of type
> character varying

I don't get that ...

I wonder whether the problem is that you are expecting the "as" labels
in the SELECT to do something, like make the data go into the
correspondingly named columns of the target table.  But they don't do
anything.  This query is going to try to assign 666, phone, and name to
the first three columns of "data", in order.  I'll bet it is actually
complaining about the second or third column, not the 666 at all.

If that's it, the syntax you are looking for is

insert into data (block_number, phone_number, name)
    select cast('666' as integer), phone_number, name
from demo_q;

            regards, tom lane

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

Предыдущее
От: Joshua Drake
Дата:
Сообщение: Re: pg_start_backup() takes too long
Следующее
От: Tom Lane
Дата:
Сообщение: Re: database question