Extract data in parentheses with Amazon-redshift

2019-08-29 03:57发布

I have these characters in a table:

LEASE THIRD-CCP-MANAGER (AAAA)
THE MANAGEMENT OF A THIRD PARTY (BBBB / AAAA)

When I extract the information:

AAAA
BBBB/AAAA

That is, I have to look for the pattern and extract what is inside the parenthesis.

I'm trying to use the REGEXP_SUBSTR function.

In amazon redshift, how do I extract the characters in parentheses?

thanks

3条回答
Explosion°爆炸
2楼-- · 2019-08-29 04:41

Whoa, that was hard!

Here is the syntax to use:

SELECT REGEXP_SUBSTR('One (Two) Three', '[(](.*)[)]', 1, 1, 'e')

This will return: Two

It appears that escaping brackets with \( doesn't work, but putting them in [(] does work. The 'e' at the end will "Extract a substring using a subexpression".

查看更多
何必那么认真
3楼-- · 2019-08-29 04:50

use position for finding the index of parenthesis ( and then substring

select 
substring(position('(' in 'LEASE THIRD-CCP-MANAGER (AAAA)'),position(')' in 'LEASE THIRD-CCP-MANAGER (AAAA)'))

or you can use split_part

split_part('LEASE THIRD-CCP-MANAGER (AAAA)','(',2)
查看更多
我想做一个坏孩纸
4楼-- · 2019-08-29 05:03

You’re probably struggling with () meaning something in regular expressions (lookup “back references”)

To tell regex that you just mean the characters ( and ) without their special meaning, “escape” them using \

regexp_substr(yourtable.yourcolumn,'\(.*\)')
查看更多
登录 后发表回答