Solved

"Backwards" TSQL string search

Posted on 2014-04-03
4
389 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 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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
find SQL job run average duration 24 55
MS SQL Sever Import/export problem 7 47
SQL Query Task 11 43
SQL 2012 Instance Problem 3 55
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

749 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