PostgreSQL拆分字符串的三种方式

来自:网络
时间:2024-03-31
阅读:
免费资源网 - https://freexyz.cn/

方式一:

字符串转为数组 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拆分字符串的三种方式

免费资源网 - https://freexyz.cn/
返回顶部
顶部