Thread

  1. cast bit to boolean?

    Vivek Khera <khera@kcilink.com> — 2001-05-03T21:25:14Z

    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.
    
    Thanks.
    
    
  2. Re: cast bit to boolean?

    Joel Burton <jburton@scw.org> — 2001-05-03T21:42:30Z

    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@scw.org>
    Director of Information Systems, Support Center of Washington
    
    
    
  3. Re: cast bit to boolean?

    Robert Hentosh <hentosh@io.com> — 2001-05-03T22:02:00Z

    On Thu, May 03, 2001 at 05:25:14PM -0400, 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.
    > 
    > Thanks.
    
    Is this what you are trying to accomplish?
    
    select * from t1 where ( sel_a & b'100001' <> b'000000' );
    
    
  4. Re: cast bit to boolean?

    Robert Hentosh <hentosh@io.com> — 2001-05-03T22:08:14Z

    On Thu, May 03, 2001 at 05:25:14PM -0400, 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.
    > 
    > Thanks.
    
    If you want only the rows where both bits are set then try:
    
    select * from t1 where ( sel_a & b'100001' = b'100001' );
    
    It seems that all bit files must be equal in length.
    
    
    
  5. Re: cast bit to boolean?

    Tom Lane <tgl@sss.pgh.pa.us> — 2001-05-03T23:09:13Z

    Robert Hentosh <hentosh@io.com> writes:
    > If you want only the rows where both bits are set then try:
    >
    > select * from t1 where ( sel_a & b'100001' = b'100001' );
    
    Or for the other case,
    
    select * from t1 where ( sel_a & b'100001' != b'000000');
    
    			regards, tom lane
    
    
  6. Re: cast bit to boolean?

    Vivek Khera <khera@kcilink.com> — 2001-05-04T13:54:51Z

    >>>>> "RH" == Robert Hentosh <hentosh@io.com> writes:
    
    RH> select * from t1 where (sel_a & b'100001' <> b'000000' );
    
    Yep.  That solution hit me just as the head hit the pillow last night,
    too!
    
    Thanks.