Ad

Get The 3rd Part Of The String In Postgres

- 1 answer

I have data like,

ab-volt-ssn-dev
ab-volt-lnid-dev
ab-volt-ssn-hamp-dev
ab-volt-cf-apnt-test

I need output to be like,

ssn
lnid
ssn
cf
Ad

Answer

You can use split_part()

select split_part('ab-volt-ssn-hamp-dev', '-', 3);

If you need to access multiple parts, then converting it to an array might be easier:

select elements[1],
       elements[2],
       elements[3],
       elements[4]
from (
   select string_to_array(the_column, '-') as elements
   from the_table
) t;          
Ad
source: stackoverflow.com
Ad