postgresql regexp_split_to_table函数

在从Oracle切换到postgresql数据库的系统改造中,遇到字段数据格式为A|B|C的问题,需要将其转换为钻石|铂金|黄金。通过使用postgresql的regexp_split_to_table函数,成功实现了数据的拆分与转换。查询过程包括replace函数替换竖线为逗号,再结合regexp_split_to_table拆分结果集,最后用string_agg函数合并成字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近公司系统在做改造,数据库产品也换了,由Oracle数据库换成postgresql 数据库。

 

在开发的过程中,同事遇到一问题,就是有张表有个字段的数据存储格式是 

A|B|C 这种形式, 现在要专业这种形式 钻石|铂金|黄金。

A对应的是钻石

B对应的是铂金

C对应的是黄金

 

刚开始以为要自定义一个函数,实现这样一个转换功能,偶然在qq群看到 pg有 regexp_split_to_table 函数。于是看是做实验测试:

--建表

drop table if exists test1;
drop table if exists test2;
create table test1(tier_code varchar(100));
create table test2(tier_code varchar(100),tier_name varchar(100));

--测试数据

insert into test1(tier_code) values ('A|B');
insert into test1(tier_code) values('A|B|C');
insert into test1(tier_code) values('A|B|C|E');


insert into test2(tier_code,tier_name) values('A','钻石');
insert into test2(tier_code,tier_name) values('B','铂金');
insert into test2(tier_code,tier_name) values('C','黄金');
insert into test2(tier_code,tier_name) values('E','黑钻');
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值