?
Solved

"Backwards" TSQL string search

Posted on 2014-04-03
4
Medium Priority
?
412 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
[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
4 Comments
 
LVL 13

Expert Comment

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

Accepted Solution

by:
sdstuber earned 2000 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:Cornelia Yoder
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

764 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