Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a string and select nth part [duplicate]

I want to split a string using a colon as delimiter and select 2nd and 3rd part.

Table:

|asdsc:dcdes:dcd|
|dcfd:drfe:regf |

Expected output:

|dcdes|
|drfe|

and also third column:

|dcd |
|regf|
like image 902
um123 Avatar asked Dec 18 '22 05:12

um123


1 Answers

Use split_part():

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)    
like image 133
klin Avatar answered Dec 28 '22 23:12

klin