?
Solved

SQL Regex?

Posted on 2014-11-23
5
Medium Priority
?
370 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 51

Accepted Solution

by:
Vitor Montalvão earned 2000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.
Suggested Courses

752 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