From: | Thom Brown <thombrown(at)gmail(dot)com> |
---|---|
To: | Amitabh Kant <amitabhkant(at)gmail(dot)com> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: Comparing bit in an integer field |
Date: | 2009-11-16 14:16:22 |
Message-ID: | bddc86150911160616w40c75da1g5b2073afa098910@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
2009/11/16 Amitabh Kant <amitabhkant(at)gmail(dot)com>:
> Hello
>
> I need to compare the bit values of a integer field in my table. For
> example, I have a table called "t1" with just one field "a1" having
> following values:
>
> a1
> ==
> 0
> 12
> 8
> 0
> 1
> 10
> 7
> 19
>
> I am trying to fetch all records where the 3rd binary bit is 1, which from
> the above example should be 12 (00001100) and 7 (00000111). If I convert the
> values to binary and store it as string, I can easily compare them using
> substr, but would rather like to retain the integer field. I have tried
> using the get_bit function, but it seems my understanding of the function is
> not correct.
>
> I would appreciate any help in this regard.
>
>
> Amitabh
>
There's probably a clean way of doing this, but you could do:
SELECT a1
FROM t1
WHERE (a1::bit(10) & 4::bit(10))::int = 4;
a1
----
12
7
(2 rows)
Regards
Thom
From | Date | Subject | |
---|---|---|---|
Next Message | A. Kretschmer | 2009-11-16 14:23:25 | Re: Comparing bit in an integer field |
Previous Message | Amitabh Kant | 2009-11-16 14:00:48 | Comparing bit in an integer field |