Solved

SQL Regex?

Posted on 2014-11-23
5
358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 50

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

696 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