Solved

MSSql search data with "LIKE" from list of items

Posted on 2014-01-05
12
385 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
  • 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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 48

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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 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 to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

813 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

9 Experts available now in Live!

Get 1:1 Help Now