Re: cast bit to boolean?

From: Joel Burton <jburton(at)scw(dot)org>
To: Vivek Khera <khera(at)kcilink(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: cast bit to boolean?
Date: 2001-05-03 21:42:30
Message-ID: Pine.LNX.4.21.0105031740520.9166-100000@olympus.scw.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, 3 May 2001, Vivek Khera wrote:

> How might one case a BIT to a BOOLEAN? For example, I want to return
> rows which have non-zero bit representation for, say, (sel_a & b'0011').
> That is, rows with the first or second bit set.
>
> I tried an explicit CAST, and just the query directly, but the cast
> say you cant cast type 'bit' to 'bool', and the direct query says
> WHERE clause must return type bool, not type bit:
>
> create table t1 (sel_a BIT(6),sel_b BIT(6));
> insert into t1 values (b'000001',b'001000');
> select * from t1 where sel_a & b'100001';
> ERROR: WHERE clause must return type bool, not type bit
>
> Where might I look for this? The manual is quite sparse regarding BIT
> types.

There might be a better way, but you can write a conversion routine:

create function bool(bit) returns bool as '
begin
if $1 = ''1''::bit
then
return true;
end if;
return false;
end;'
language 'plpgsql' with (isacachable);

should do the trick, albeit more slowly than a built-in or C function.

--
Joel Burton <jburton(at)scw(dot)org>
Director of Information Systems, Support Center of Washington

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Nils Zonneveld 2001-05-03 21:44:59 Re: MS-Query
Previous Message Lieven Van Acker 2001-05-03 21:41:03 Re: View permissions in 7.1