Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何拆分字符串并选择第n部分_String_Postgresql_Substring - Fatal编程技术网

String 如何拆分字符串并选择第n部分

String 如何拆分字符串并选择第n部分,string,postgresql,substring,String,Postgresql,Substring,我想使用冒号作为分隔符拆分字符串,并选择第二部分和第三部分 表: |asdsc:dcdes:dcd| |dcfd:drfe:regf | 预期产出: |dcdes| |drfe| 第三栏: |dcd | |regf| 您可以使用string\u to\u array将字符串转换为数组,使用:作为分隔符来获取分隔符之间的每个元素,然后指定数组的索引以检索值。在这里,您想省略第一个,但取第二个和第三个 代码: 使用: select strarr[2], strarr[3] from ( sel

我想使用冒号作为分隔符拆分字符串,并选择第二部分和第三部分

表:

|asdsc:dcdes:dcd|
|dcfd:drfe:regf |
预期产出:

|dcdes|
|drfe|
第三栏:

|dcd |
|regf|

您可以使用
string\u to\u array
将字符串转换为数组,使用
作为分隔符来获取分隔符之间的每个元素,然后指定数组的索引以检索值。在这里,您想省略第一个,但取第二个和第三个

代码:

使用:

select strarr[2], strarr[3]
from (
select string_to_array(string_col, ':') as strarr
from yourtable
  ) t
with my_table(str) as (
    values 
    ('asdsc:dcdes:dcd'),
    ('dcfd:drfe:regf')
)

select 
    str, 
    split_part(str, ':', 2) as part_no_2, 
    split_part(str, ':', 3) as part_no_3
from my_table

       str       | part_no_2 | part_no_3 
-----------------+-----------+-----------
 asdsc:dcdes:dcd | dcdes     | dcd
 dcfd:drfe:regf  | drfe      | regf
(2 rows)