Link to home
Start Free TrialLog in
Avatar of whorsfall
whorsfallFlag for Australia

asked on

SQL Regex?

Hi,

I am trying to work out a SQL regex match for a field.

So lets say I have a field called data1 that contains:

select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client from SMS_R_System where SMS_R_System.SystemGroupName = "AAA\\AdGroup1"

I want to be able to do a T-SQL select statement that will just extract the string AdGroup1
from the above expression.

So how can I get a capture group. So to use a regex like below and just get the capture group:
.*SMS_R_System.SystemGroupName.*\\(.*)\"

Thanks,

Ward.
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

We need to confirm we understand the logic with which you want to use to extract the value.

Assuming your pattern describes pretty much what you want, it seems to be the value in the string between the two backslashes and the closing double quote, following the string "SMS_R_System.SystemGroupName".

I'm not a T-SQL guru, but I'm pretty sure you need a Regular Expressions add-on/plug-in before you can do them in T-SQL. If you don't want to go down that track, you could potentially use use some clever logic with CHARINDEX and SUBSTRING to extract the value.
Avatar of whorsfall

ASKER

Hi,

Yes your understanding is correct. Would pat index work?
With some accuracy, depending on your data. You'd perhaps look for an occurrence of \\ which is within x many characters of the occurrence of "SMS_R_System.SystemGroupName", then find the occurrence of the double quote at the end to work out how many characters to take as a substring.
ASKER CERTIFIED SOLUTION
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial