Home > Software design >  T-sql PATINDEX with regex
T-sql PATINDEX with regex

Time:08-03

Could you help me with this problem?

I have a string:

ECTB-3_ABC1192103_8G1D

I want to get this:

ABC1192103_8

I tried patindex with this pattern to get the starting position of the string:

[a-zA-Z]{3}[0-9] /[0-9] 


PATINDEX( '%[a-zA-Z]{3}[0-9] /[0-9] %', 'ECTB-3_ABC1192103_8G1D')

But it returns 0.

CodePudding user response:

With your shown samples and attempts please try regex. This will create 1 capturing group which will have the expected value in it.

^[a-zA-Z] -[0-9]_([[:alnum:]] _[0-9])[0-9a-zA-Z] $

Here is the Online demo for above regex.

CodePudding user response:

U can use this code and try on regex101 site.

SQL>
select col, regexp_substr(col,'\d\w [a-zA-Z0-9]_\d') as result
from 
(
select 'ECTB-3_ABC1192103_8G1D' as col from dual)
SQL>
  • Related