Solved

Reverse searching text string in SQL

Posted on 2016-08-31
6
42 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 51

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 51

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Stored Proc - Rewrite 42 79
SQL Recursion 6 49
T-SQL: Need a database plan to mine a pretty big Log table 4 51
get count of orders by customer Sql Server table. 3 48
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

710 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