ZedZip
ZedZip

Reputation: 6468

How to select values into var using BIT operation?

I have a table

create table t
(id serial primary key,
name text,
val INTEGER);


insert into t(name, val)
select 'user1', x'0001'::INT
union all
select 'user1', x'0010'::INT
union all
select 'user1', x'0110'::INT
union all
select 'user2', x'0001'::INT

How I can select values into variable using bit operation for given name?

create or replace function get_union(
   name text,
   OUT retval int
)
as $BODY$
begin
-- ?
end
$BODY$ language plpgsql;

For example, the function should return 111 for name 'user1'

select to_hex(x'0001'::int | x'0010'::int | x'0100'::int);
---
111

Upvotes: 0

Views: 171

Answers (1)

Clodoaldo Neto
Clodoaldo Neto

Reputation: 125304

select to_hex(bit_or(val)), bit_or(val), bit_or(val)::bit(16)
from t
where name = 'user1';
 to_hex | bit_or |      bit_or      
--------+--------+------------------
 111    |    273 | 0000000100010001

Upvotes: 1

Related Questions