加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

数组 – 在PostgreSQL中将位字符串转换为数组

发布时间:2020-12-13 16:05:39 所属栏目:百科 来源:网络整理
导读:我有一个160个字符串的字符串,我需要一个整数数组来存储值为1的位的位置. 例: bitstring = '00110101'array = [3,4,6,8] 是否可以仅使用SQL执行此操作,还是需要定义PL / SQL函数或类似的东西? 解决方法 确实可以用SQL编写它.这是一个起点: select array(
我有一个160个字符串的字符串,我需要一个整数数组来存储值为1的位的位置.

例:

bitstring = '00110101'
array = [3,4,6,8]

是否可以仅使用SQL执行此操作,还是需要定义PL / SQL函数或类似的东西?

解决方法

确实可以用SQL编写它.这是一个起点:

select array(
  select substring(str from i for 1) as bit
  from generate_series(1,length(str)) as i
  where bit = '1'
);

不过,您可能希望将其包装在pl / sql函数中,以避免在整个地方重复代码.

工作功能:

create or replace function get_bit_positions(varbit) returns bit[] as $$
select array(
  select substring($1 from i for 1) as bit
  from generate_series(1,length($1)) as i
  where substring($1 from i for 1) = '1'
);
$$language sql immutable;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读