小智 10
# select length(replace(x::text, '0', '')) from ( values ('1010111101'::bit varying) ) as something(x);
length
--------
7
(1 row)
Run Code Online (Sandbox Code Playgroud)
没有字符串转换的方法:
# select count(*) from ( select x, generate_series(1, length(x)) as i from ( values ('1010111101'::bit varying) ) as something(x) ) as q where substring(x, i, 1) = B'1';
count
-------
7
(1 row)
Run Code Online (Sandbox Code Playgroud)
如果您需要它真正高效,请进行以下讨论:Efficiently certaining the number of bits set in thecontents of, a VARBIT field
| 归档时间: |
|
| 查看次数: |
2662 次 |
| 最近记录: |