0

How to perform below operation using teradata,

Input string- 'data1 data2 (1) (Ab-123)'

output required- Ab-123

So basically I want data from last bracket.

Igor
  • 60,821
  • 10
  • 100
  • 175

1 Answers1

1

This looks like a regular expression:

select regexp_substr('data1 data2 (1) (Ab-123)',
                      '[(]([^)]*)[)]$'
                     )
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • 1
    I believe it's `regexp_substr` in Teradata; `SELECT REGEXP_SUBSTR('data1 data2 (1) (Ab-123)', '[(]([^)]*)[)]$');` Although that holds on to the parentheses, so maybe something like `SELECT REGEXP_REPLACE('data1 data2 (1) (Ab-123)', '^.*[(]([^)]*)[)]$', '\1');` – JNevill Aug 18 '20 at 13:25
  • @JNevill . . . What do you know! Teradata has rebranded Presto's documentation: https://teradata.github.io/presto/docs/148t/functions/regexp.html. Because the function varies across databases, I usually check before answering (yeah I sometimes do that). It doesn't always work, apparently. – Gordon Linoff Aug 18 '20 at 14:19
  • 1
    I've never been out to the presto documentation. It's much nicer than Teradata's knowledge base! You are about to crack 1 million! We need to pop some champagne! – JNevill Aug 18 '20 at 14:54
  • 1
    @JNevill: The Presto docu might be nicer, but it's Presto syntax, not Teradata :-) – dnoeth Aug 18 '20 at 20:04
  • @JNevil Thanks. It works perfectly. Any reference doc or link to understand this? – Ankush Gondane Aug 19 '20 at 01:53
  • @Gordon Linoff Presto documentations not work always due to syntax. – Ankush Gondane Aug 19 '20 at 01:56
  • @AnkushGondane . . . You're missing the point. I searched for Teradata documentation, but Presto came up. – Gordon Linoff Aug 19 '20 at 14:36
  • @AnkushGondane. https://docs.teradata.com/reader/kmuOwjp1zEYg98JsB8fu_A/bSkgRmSt8P48II0SKyyH3Q is a good source. That's specifically around `regexp_subtr()` which is a super handy function. The ugly stuff `'^.*[(]([^)]*)[)]$'` is regex code which you can hit up any regex tutorial/learning site to understand. – JNevill Aug 19 '20 at 20:38