Solved

MSSql search data with "LIKE" from list of items

Posted on 2014-01-05
12
395 Views
Last Modified: 2014-01-15
Hi Experts

I would like to ask if there are any query programming that I can query data from "Like" command from the list of items as follows example:

MatCode
1) RAY9994.RPL0001.RPL0002
2) RAY9993.RPL0001.RPL0005
3) RAY9994.RPL0002.RPL0004
4)RAY9995.RPL0001.RPL0005
5)RAY9996.RPL0003.RPL0005

SearchCode
1)RPL0001
2)RPL0003

Result
1) RAY9994.RPL0001.RPL0002
2) RAY9993.RPL0001.RPL0005
4)RAY9995.RPL0001.RPL0005
5)RAY9996.RPL0003.RPL0005


Thanks & Regards,

Becky
0
Comment
Question by:beckyng
[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
  • 3
  • 3
  • 2
  • +4
12 Comments
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39758617
where MatCode like '%RPL0001%' or MatCode like '%RPL0003%'

Open in new window

0
 

Author Comment

by:beckyng
ID: 39758623
Sorry, May I ask if there are over 100 items from the list, how should I do?

Thanks
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39758636
Do you mean 100 different patterns you want to search for?

If so it depends on how create your list of patterns. Are you going to type them in by hand? Do you have them in a table?
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Comment

by:beckyng
ID: 39758661
Yes, sure I have a table which contain over 100 items that wanna to search it out from the Matcode. May I ask if there are any solution that I can eliminate the MANUAL action?

Thanks & Regards,

Becky
0
 
LVL 11

Expert Comment

by:Louis01
ID: 39758679
select *
  from myTable t1
 where exists (select 1 
                 from myListTable t2 
                where t1.MatCode like '%' + t2.myCol + '%')

Open in new window

0
 
LVL 3

Expert Comment

by:13Shadow
ID: 39759360
Try:
SELECT * FROM table WHERE CONTAINS(Matcode, ' RPL0001 or RPL0003 ')
0
 
LVL 32

Expert Comment

by:awking00
ID: 39759565
Just to clarify from your example, are you saying there is a table, SearchCode, that, in addition to RPL0001 and RPL0003, could contain 98 or more values you want to search the MatCode on?
0
 
LVL 32

Accepted Solution

by:
awking00 earned 300 total points
ID: 39759598
select * from matcode as m
inner join searchcode as s
on charindex(s.search_val,m.code) > 0;
0
 

Author Comment

by:beckyng
ID: 39759603
Yes, Actually I have a table which contain over 1million record of matcode and I want to search out from it with the table of searchcode (100 items).

In order to eliminate manual input the searchcode by using operator "LIKE" for 100 times, I would like to ask if there are some way to speed up the process.

Thanks

Becky
0
 
LVL 32

Expert Comment

by:awking00
ID: 39759652
Did you try my last suggestion? Note, I used code as the column being searched in the matcode table and search_val as the column for the items in the searchcode table. You can substitute your actual column names for those and it should work. Also note that charindex(search_val,code) > 0 is equivalent to code like %search_val%.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39760246
You could add the most efficiency by analyzing the search values and combining them when appropriate to reduce the number of LIKEs.

For example, from the two search values you've specified so far, you can do this one LIKE:

WHERE
    MatCode LIKE '%RPL000[13]%'

Of course that may not be practical if you need a fully automated process to do this search with values that change for each search.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39761467
MatCode appears to have 3 significant parts

1) RAY9994   .RPL0001    .RPL0002  --<< RPL0002/part 3
2) RAY9993   .RPL0001    .RPL0005
3) RAY9994   .RPL0002    .RPL0004 --<< RPL0002/part 2
4) RAY9995   .RPL0001    .RPL0005
5) RAY9996   .RPL0003    .RPL0005

are you expecting to match to only the second (middle) part?

e.g. if "RPL0002" was in the table of searchcodes, what would be the expected result?

1) & 3)
3) only
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

623 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