Solved

SQL Regex?

Posted on 2014-11-23
5
338 Views
Last Modified: 2014-12-27
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.
0
Comment
Question by:whorsfall
  • 2
5 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 40461605
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.
0
 

Author Comment

by:whorsfall
ID: 40461640
Hi,

Yes your understanding is correct. Would pat index work?
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 40461761
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.
0
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40461805
For this particular case:
SELECT SUBSTRING(@MySQL,CHARINDEX('\\',@MySQL,CHARINDEX('SMS_R_System.SystemGroupName',@MySQL))+2,LEN(@MySQL)-CHARINDEX('\\',@MySQL,CHARINDEX('SMS_R_System.SystemGroupName',@MySQL))-2)

Open in new window

@MySQL contains the string where you want to look for and you can replace it with a column name if you are going to use it on a query.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now