PostgreSQL拆分字符串的三种方式
方式一:
字符串转为数组 string_to_array
、 regexp_split_to_array
string_to_array(‘待分割字符串’,‘分割符’)
regexp_split_to_array(‘待分割字符串’,E’正则表达式’)
select string_to_array('https://www.douban.com/gallery/topic/305785','/') as strings 或 select regexp_split_to_array('https://www.douban.com/gallery/topic/305785',E'\\/') as strings
查询结果:
获取数组元素
strings[1]、strings[2]、strings[3]、strings[4]、strings[5]、strings[6]
不用担忧数组越界问题
select strings[1],strings[2],strings[3],strings[4],strings[5],strings[6] from (select string_to_array('https://www.douban.com/gallery/topic/305785','/') as strings ) foo
查询结果:
方式二:
字符串转为列表 regexp_split_to_table
regexp_split_to_table(‘待分割字符串’,‘分割符’)
regexp_split_to_table(‘待分割字符串’,E’正则表达式’)
select * from regexp_split_to_table('https://www.douban.com/gallery/topic/305785','/') 或 select * from regexp_split_to_table('https://www.douban.com/gallery/topic/305785',E'\\/')
查询结果:
方式三:
字符串转为数据项 split_part
split_part(‘待分割字符串’,‘分割符’,第几项)
--获取第一项 select split_part('https://www.douban.com/gallery/topic/305785', '/', 1)
查询结果:
到此这篇关于PostgreSQL拆分字符串的三种方式的文章就介绍到这了,更多相关PostgreSQL拆分字符串内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Postgresql的pl/pgql使用操作--将多条执行语句作为一个事务
这篇文章主要介绍了Postgresql的pl/pgql使用操作--将多条执行语句作为一个事务,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2021-01-01Postgresql排序与limit组合场景性能极限优化详解
这篇文章主要介绍了Postgresql排序与limit组合场景性能极限优化详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-12-12
最新评论