Solved

Reverse searching text string in SQL

Posted on 2016-08-31
6
43 Views
Last Modified: 2016-09-16
Hello Experts,

I'm after some code to use in SQL Server which allows to pick up 2 different user id's stored within a text string.

I have code which will let me pick up the first ID moving left to right but i'd like to pick up the 2nd id moving from right to left.

The text string will be something like 'Name D239383 Name D938373'

I'm using the below code to get the first UID D239383:
Case When string Like '%D[0-9]%' then 'D'+SUBSTRING(string,PATINDEX('%D[0-9]%',string)+1,6) ELSE Null END

replacing 'string' with the proper field name.

I tried using the REVERSE command but couldn't seem to get it to work.

And assistance would be greatly appreciated!
0
Comment
Question by:Chris_Sizer
[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
  • 2
  • 2
6 Comments
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41777692
quick try:
declare @string nVarchar(100)
set @string =  'Name D239383 Name D938373'

select @string, parsename(replace(@string, 'Name', '.'), 1)

Open in new window

0
 

Author Comment

by:Chris_Sizer
ID: 41777712
Hi Ryan,

Thanks for the reply!
If i understand this correctly, you are looking to replaced the word Name with a '.'?

If that's the case i'm not sure this will work as a team members name will be different in each instnace.

Also, how would i bring through the column name in your example?
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41777762
>>If i understand this correctly, you are looking to replaced the word Name with a '.'?
yes, that's the idea when we are using function parsename.

>>If that's the case i'm not sure this will work as a team members name will be different in each instnace.
do you have other instances can show here?
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 52

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 41777766
Hi,

pls try

Case When string Like '%D[0-9]%D[0-9]%' then 'D'+SUBSTRING(SUBSTRING(string,PATINDEX('%D[0-9]%',string)+1,LEN(string)),PATINDEX('%D[0-9]%',SUBSTRING(string,PATINDEX('%D[0-9]%',string)+1,Len(string))+1,6) ELSE  Null END

Open in new window

Regards
0
 

Author Comment

by:Chris_Sizer
ID: 41777808
Hi Rgonzo1971,

This is working well, but it seems to be bringing through part of the 2nd name as well as the remainder of the text string.

Any ideas?
0
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 41777838
then try
select Case When string Like '%D[0-9]%D[0-9]%' then 'D'+SUBSTRING(SUBSTRING(string,PATINDEX('%D[0-9]%',string)+1,LEN(string)),PATINDEX('%D[0-9]%',SUBSTRING(string,PATINDEX('%D[0-9]%',string)+1,Len(string)))+1,6) ELSE  Null END

Open in new window

0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

615 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