首页 > 数据库    PostgreSQL日期:2024-03-31 / 浏览

方式一:

字符串转为数组 string_to_arrayregexp_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

查询结果:

PostgreSQL拆分字符串的三种方式

获取数组元素

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

查询结果:

PostgreSQL拆分字符串的三种方式

方式二:

字符串转为列表 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'\\/')

查询结果:

PostgreSQL拆分字符串的三种方式

方式三:

字符串转为数据项 split_part

split_part(‘待分割字符串’,‘分割符’,第几项)

--获取第一项
select split_part('https://www.douban.com/gallery/topic/305785', '/', 1) 

查询结果:

PostgreSQL拆分字符串的三种方式

觉得上面的内容有用吗?快来点个赞吧!

点赞() 我要打赏

温馨提示 : 本站内容来自会员投稿以及互联网,所有源码及教程均为作者总结编辑,请大家在使用过程中提前做好备份,以免发生无法预知的错误,源码类教程请勿直接用于生产环境!

 可能感兴趣的文章