Solved

MSSql search data with "LIKE" from list of items

Posted on 2014-01-05
12
392 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query help 3 24
SCOM to SQL port 14xx failed? 1 29
SQL Server Resume 5 44
Database Owner 3 13
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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.

713 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