Re: Type coercion on column in a query

Поиск
Список
Период
Сортировка
От Richard Huxton
Тема Re: Type coercion on column in a query
Дата
Msg-id 200305141732.11386.dev@archonet.com
обсуждение исходный текст
Ответ на Re: Type coercion on column in a query  (Randall Lucas <rlucas@tercent.net>)
Ответы Re: Type coercion on column in a query
Список pgsql-sql
On Wednesday 14 May 2003 5:10 pm, Randall Lucas wrote:
> Along the same lines, is it possible to tell whether a column may be
> cast to a given type (e.g. numeric) without throwing an error?
>
> Specifically, I have some values that will probably be numbers but
> might have some free text or other gobbledygook in there.  I want to
> take the sum of all the numeric values, and maybe the average as well.
> So what I envision is:
>
> table dirty_data (
>       id                 int,
>       dirty_numbers         text
> );

> select sum( safe_numeric_cast(dirty_numbers) ) from dirty_data where
> safe_numeric_cast(dirty_numbers) is not null;

You'd have to write "safe_numeric_cast()" in plperl, or possibly pltcl (sorry,
I don't use TCL). There's no exception handling in Postgresql (nor will there
be in the next few months).

Alternatively, you could conceivably write a bunch of is_numeric(), is_int()
etc functions and test first.

--  Richard Huxton


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

Предыдущее
От: Randall Lucas
Дата:
Сообщение: Re: Type coercion on column in a query
Следующее
От: Larry Rosenman
Дата:
Сообщение: Followup from yesterday's PL/pgSQL fun...