Re: How to count ones in a bit string?

From: Richard Broersma <richard(dot)broersma(at)gmail(dot)com>
To: Cheng <niechenghust(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: How to count ones in a bit string?
Date: 2012-02-21 15:44:22
Message-ID: CABvLTWHuX7pDAzQH9bYwBp9qKfsDJfaCx9DVn1oraG8yR5sz9Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

There are probably fast better ways. But this is one way:

postgres=> SELECT LENGTH( REPLACE( CAST( B'101000000000000000000010'
AS TEXT ), '0', ''));
length
--------
3

On Tue, Feb 21, 2012 at 7:32 AM, Cheng <niechenghust(at)gmail(dot)com> wrote:
> For example, I have a bit string as:
>
> B'101000000000000000000010'
>
> How do I count the number of ones in it? In this example, it is 3.
>
> Thanks,
> Cheng
>
> --
> Sent via pgsql-novice mailing list (pgsql-novice(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-novice

--
Regards,
Richard Broersma Jr.

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Michael Wood 2012-02-21 15:52:12 Re: How to count ones in a bit string?
Previous Message Cheng 2012-02-21 15:32:51 How to count ones in a bit string?