Re: can't cast varchar as integer?

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: can't cast varchar as integer?
Дата
Msg-id 1600.1021418950@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Re: can't cast varchar as integer?  (David Stanaway <david@stanaway.net>)
Список pgsql-sql
David Stanaway <david@stanaway.net> writes:
>> LEDEV=3D# select cast(foo as integer) from test;
>> ERROR:  Cannot cast type 'character varying' to 'integer'

> scratch=3D# select foo::text::integer from test;
> [works]

> scratch=3D# select int4(foo) from test;
> [works]

For reasons that I don't entirely recall at the moment (but they seemed
good to the pghackers list at the time), cast notations only work if
there is a cast function *exactly* matching the requested cast.
On the other hand, the functional form is laxer because there's an
allowed step of implicit coercion before the function call.

In the case at hand, there's a text->int4 cast function (look in
pg_proc, you'll see int4(text)) but there's no int4(varchar) function.
Also, varchar can be cast to text implicitly --- this is actually
a "binary equivalent" cast requiring no run-time effort.  Soselect foo::text::integer from test;
works: it's a binary-equivalent cast from varchar to text, followed
by application of int4(text).  And select int4(foo) from test;
works because the same function is found and implicit coercion of
its argument to text succeeds.  Butselect cast(foo as integer) from test;
doesn't work because there's no declared function int4(varchar).

There's probably not any good reason why there's not int4(varchar),
just that no one got around to making one.
        regards, tom lane


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

Предыдущее
От: Scott Royston
Дата:
Сообщение: Re: can't cast varchar as integer?
Следующее
От: "Christopher Kings-Lynne"
Дата:
Сообщение: Re: order by