1

I have a Column which have string values in my PostgreSQL table.

The string looks like this:

'192.168.1.1-mike.landline,192.136.152-sam.phone,192.364.1.0-main-phone'

Based on the string how can I select the last most value before the comma starts. i.e. 192.364.1.0-main-phone

I tried to look it up but no luck so far

1 Answer 1

1

Use the function regexp_replace().

select regexp_replace('192.168.1.1-mike.landline,192.136.152-sam.phone,192.364.1.0-main-phone', '.*,', '');

     regexp_replace     
------------------------
 192.364.1.0-main-phone
(1 row) 

Read about POSIX Regular Expressions in the documentation.


Update. You can trim commas ending a string with the function rtrim(), e.g.:

select rtrim('some_string,', ',')

    rtrim    
-------------
 some_string
(1 row)

so your query may look like this:

select regexp_replace(rtrim(the_string, ','), '.*,', '');
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @klin for answering my question. Can you please help me with one more problem. The problem is that in some of my data my strings ends with a comma for an example '192.364.1.0-main-phone,'. How can I select the last value in that case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.