Search code examples
stringpostgresqlsubstring

Get the 3rd part of the string in Postgres


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

Solution

  • 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;