Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reverse searching text string in SQL

Posted on 2016-08-31
6
Medium Priority
?
50 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 53

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 53

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
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 
LVL 52

Accepted Solution

by:
Rgonzo1971 earned 2000 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

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

688 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