Solved

"Backwards" TSQL string search

Posted on 2014-04-03
4
384 Views
Last Modified: 2014-04-03
I need to do a kind of "backwards" substring search in a SQL 2008R2 database.  The column I need to search contains a subset of a tracking number, with a minimum of 10 characters.  When a package arrives, it is scanned, but the result is a value which may have many more characters.   What I need to know is whether that scanned number contains any of the strings in the table column of interest.

So, the user enters '1234567890' in the table column.  A package comes in and the tracking number is 'ABCD1234567890WXZ'.   I want that to be a "hit".

It's easy to do it the other way (i.e. if the full tracking # was in the table and I was searching for a substring in my query), but my brain is coming up empty on figuring out a good way to do this.  The table has a couple hundred thousand rows and I don't think my client will do a full text index.  The solution needs to be reasonably efficient and fast.

Can anyone unclog my brain?   This should be easy!

Thanks,

Bob
0
Comment
Question by:BobSacks
4 Comments
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 39974880
select * from table where tracking_number_field like '%1234567890%'
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 39974888
searching on the middle of a string isn't going to be efficient but the syntax is easy


select * from your_table where 'ABCD1234567890WXZ' like '%' + your_column + '%'

or

select * from your_table where charindex(your_column, 'ABCD1234567890WXZ') > 0
0
 
LVL 27

Expert Comment

by:yodercm
ID: 39974890
SELECT * FROM Table WHERE  INSTR(Column,'1234567890') >0
0
 

Author Comment

by:BobSacks
ID: 39974973
Thanks to those who responded.  I guess my question wasn't well stated.  Couple of you suggested solutions which assumed that I HAD the target substring to submit to the query.  I don't.   I have only what gets scanned at the time the package is received.

SDSTUBER - I tried what you suggested.  It works.
select * from your_table where 'ABCD1234567890WXZ' like '%' + your_column + '%'

There are two problems.  As you suggested it is really slow and it hits on a blank column.  Adding a filter for  your_column  <> ''  and changing the SELECT to SELECT TOP 1 seems to address both issues.

You've given me a place to start.  Thanks!
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how the fundamental information of how to create a table.

911 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

14 Experts available now in Live!

Get 1:1 Help Now