1

I am trying to extract text between parenthesis from a column in postgres table. I am using following command. It is creating an additional blank column.

       SELECT *, SUBSTRING (col2, '\[(.+)\]') FROM table

My table looks like this:

col1   col2
1      mut(MI_0118)
2      mut(MI_0119)
3      mut(MI_0120)

My desired output is:
col1   col2
1      MI_0118
2      MI_0119
3      MI_0120

How can I extract the text without creating an additional column.

Thanks

0

2 Answers 2

2

Your regex is wrong, that's why you get an empty column. You don't want square brackets, but parentheses around the search string

select col1, substring(col2, '\((.+)\)')
from input

Online example

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

Comments

0

The * in the SELECT statement is including all columns. Then you are adding another unnamed column. If you do:

SELECT col1, SUBSTRING (col2, '\[(.+)\]') AS col2 FROM table

It will be closer to what you want.

2 Comments

It is only capturing the open parenthesis.
I can't test this right now, but your regex isn't quite right. It should match your string and you will have to escape the parentheses. Something like: .+((.+))

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.