Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

"Backwards" TSQL string search

Posted on 2014-04-03
4
Medium Priority
?
417 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

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. …
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

636 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