0

I am writing a Postgresql query to return everything within parentheses inside a string. The string is "Account Suspended - Labeler has tumbling names with (dca-5:anna williams,dca-6:Anna Williams)." so the ideal output should be dca-5:anna williams,dca-6:Anna Williams.

I have tried the following syntax:

SELECT SUBSTRING(SUBSTRING(an.comment,')',1),'(',-1) as new_comment
FROM public.akon_notes an
where an.comment like '%Account Suspended - Contributor has tumbling names with%'

But it returned the error message Amazon Invalid operation: invalid input syntax for integer: ")";

Could somebody advise what I did wrong there?

1
  • your example and your code won't work together -- the like statement will not find that string -- is this as intended? Commented Dec 6, 2019 at 17:42

4 Answers 4

2

One method is to use split_part(). Assuming you have no other parentheses in the string:

select split_part(replace(an.comment, ')', '('), '(', 2)
from (values ('"Account Suspended - Labeler has tumbling names with (dca-5:anna williams,dca-6:Anna Williams)."'::text)) an(comment);

split_part() splits the string based on a delimited. Alas, you have two delimiters. so this replaces the ) with (. Because you want the first part after the '(', this is the second part of the string. The above can be confused by the presence of other parens in the comment -- but it may be unclear how to handle that.

Another method uses regexp_replace():

select regexp_replace(an.comment, '.*[(](.*)[)].*', '\1')
from (values ('"Account Suspended - Labeler has tumbling names with (dca-5:anna williams,dca-6:Anna Williams)."'::text)) an(comment);

This is more handy than regexp_substr() because you don't have deal with the leading and trailing parens.

This does what you want more directly. The pattern matches the entire string (it could be `'^.([)].$' to be more explicit about that). The first and last parens are escaped. The part in the middle is identified as an element in the string.

The "replace" part of regexp_replace() replaces the entire string (because the pattern matches the entire string) with the first element matched (which is the part in parentheses).

Sign up to request clarification or add additional context in comments.

Comments

1

You can also use regex

substring(field,'\((.*)\)')

I didn't test you might need

substring(field,'.*\((.*)\).*')

Comments

1

Try this. The below code used Instr(..) perhaps is general n should work in postgresql or can replace instr() with strpos()

    Select.... 
   SUBSTRING(an.comment, 
    INSTR(an.comment,'(')+1,
         INSTR(an.comment,')')-1)
       FROM TABLE like... 

Comments

1

I could not test this now, but I think that you can use regex for that:

SELECT substring(an.comment from '\((.*)\)') as new_comment
FROM public.akon_notes an
where an.comment like '%Account Suspended - Contributor has tumbling names with%'

Comments

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.