Thread: cannot cast type boolean to text?

cannot cast type boolean to text?

From
Brian Hurt
Date:
I'm not sure if this is the right
So when I do:
    SELECT true :: text;
in psql, I get back:

ERROR:  cannot cast type boolean to text

I was wondering why this is so.  Unfortunately, the place where I hit
the problem was in a function that takes arguments of type ANYELEMENT
and wants to convert them to text.  Which doesn't make it impossible to
work around this, just more annoying.

This is Postgres 8.1.4.

Would this be something worthwhile to post to Hackers?

Brian


Re: cannot cast type boolean to text?

From
Tom Lane
Date:
Brian Hurt <bhurt@janestcapital.com> writes:
>     SELECT true :: text;
> ERROR:  cannot cast type boolean to text

> I was wondering why this is so.

Because there's no pg_cast entry for this conversion.

There will be one in 8.3, but for older releases make your own with
CREATE CAST.

            regards, tom lane

Re: cannot cast type boolean to text?

From
"Shoaib Mir"
Date:
You can do this by creating a custom cast for it.

--
Shoaib Mir
EnterpriseDB (www.enterprisedb.com)

On 6/14/07, Brian Hurt <bhurt@janestcapital.com> wrote:
I'm not sure if this is the right
So when I do:
    SELECT true :: text;
in psql, I get back:

ERROR:  cannot cast type boolean to text

I was wondering why this is so.  Unfortunately, the place where I hit
the problem was in a function that takes arguments of type ANYELEMENT
and wants to convert them to text.  Which doesn't make it impossible to
work around this, just more annoying.

This is Postgres 8.1.4 .

Would this be something worthwhile to post to Hackers?

Brian


---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Re: cannot cast type boolean to text?

From
"Shoaib Mir"
Date:
This should help:

create or replace function bool_to_text (boolean)
    returns char
    strict
    language sql as '
    select case
        when $1 then \'t\'
        else \'f\'
        end;
    ';

create cast (boolean as char(1))
    with function bool_to_text(boolean)
    as implicit;

--
Shoaib Mir
EnterpriseDB (www.enterprisedb.com)

On 6/14/07, Tom Lane < tgl@sss.pgh.pa.us> wrote:
Brian Hurt < bhurt@janestcapital.com> writes:
>     SELECT true :: text;
> ERROR:  cannot cast type boolean to text

> I was wondering why this is so.

Because there's no pg_cast entry for this conversion.

There will be one in 8.3, but for older releases make your own with
CREATE CAST.

                        regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

               http://archives.postgresql.org