Search code examples
stringpostgresqlpostgresql-9.3

Display the specific part of the string in PostgreSQL 9.3


I have a string to modify as per the requirements.

For example:

The given string is:

str1 varchar = '123,456,789';

I want to show the string as:

'456,789'

Note: The first part (delimited) with comma, I want to remove from string and show the rest of string.

In SQL Server I used STUFF() function.

SELECT STUFF('123,456,789',1,4,'');

Result:

456,789

Question: Is there any string function in PostgreSQL 9.3 version to do the same job?


Solution

  • you can use regular expressions:

    select substring('123,456,789' from ',(.*)$');
    

    The comma matches the first comma found in the string. The part inside the brackets (.*) is returned from the function. The symbol $ means the end of the string.

    A alternative solution without regular expressions:

    select str, substring(str from position(',' in str)+1 for length(str)) from 
    (select '123,456,789'::text as str) as foo;