Re: problem with bit(n) type - Mailing list pgsql-general

From Martijn van Oosterhout
Subject Re: problem with bit(n) type
Date
Msg-id 20051012134339.GD20074@svana.org
Whole thread Raw
In response to problem with bit(n) type  (Sergey Karin <sergey.karin@gmail.com>)
List pgsql-general
On Wed, Oct 12, 2005 at 12:24:41PM +0400, Sergey Karin wrote:
> But if I do something like this:
> kosten=# select t::text from foo;
> ERROR:  cannot cast type bit to text
>
> I get an error. Okay the system cannot  cast bit to text...
> But this is the big problem for me... Is there a way to cast bit(n) to
> text or varchar?

You found the conversion functions, and you can use them to work around
this like follows:

test=# select textin(bit_out(b'1010'));
 textin
--------
 1010
(1 row)

So perhaps:

test=# create function bit_to_text (bit) returns text
as 'select textin(bit_out($1))' language sql strict immutable;
CREATE FUNCTION
test=# select substr( bit_to_text( b'10110001' ), 3, 3 );
 substr
--------
 110
(1 row)

Hope this helps,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.

Attachment

pgsql-general by date:

Previous
From: Lincoln Yeoh
Date:
Subject: Re: Row level locking
Next
From: Ron Mayer
Date:
Subject: Re: PostgreSQL 8.1 vs. MySQL 5.0?