Postgres中的函数将varchar转换为大整数

1 postgresql

我在Postgres 8.3中有一个varchar列,它包含如下值:'0100011101111000'

我需要一个函数,将该字符串视为基数为2的数字,并在基数10中吐出数字.有意义吗?

所以,例如:

'000001' - > 1.0

'000010' - > 2.0

'000011' - > 3.0

谢谢!

Ste*_*nne 5

转换为位字符串然后转换为整数.

一个例子: '1110'::bit(4)::integer- > 14

虽然你有不同长度的例子,并且在bigint之后,所以使用函数使用bit(64)并用零填充输入lpad.

lpad('0100011101111000',64,'0')::bit(64)::bigint

这是一个完整的例子......

create temp table examples (val varchar(64));

insert into examples values('0100011101111000');
insert into examples values('000001');
insert into examples values('000010');
insert into examples values('000011');

select val,lpad(val,64,'0')::bit(64)::bigint as result from examples;
Run Code Online (Sandbox Code Playgroud)

选择的结果是:

       val        | result 
------------------+--------
 0100011101111000 |  18296
 000001           |      1
 000010           |      2
 000011           |      3
(4 rows)
Run Code Online (Sandbox Code Playgroud)