Solved

Reverse searching text string in SQL

Posted on 2016-08-31
6
39 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
  • 2
  • 2
  • 2
6 Comments
 
LVL 50

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 50

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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 49

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 49

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with t-sql finding "latest" record in table 10 33
SQL Server 2012 rs - Field1 and Field2 add 1 to Total 4 17
MS SQL Delete Query 9 29
SQL Error - Query 6 24
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

813 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

11 Experts available now in Live!

Get 1:1 Help Now