Solved

MSSql search data with "LIKE" from list of items

Posted on 2014-01-05
12
377 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
Comment Utility
where MatCode like '%RPL0001%' or MatCode like '%RPL0003%'

Open in new window

0
 

Author Comment

by:beckyng
Comment Utility
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
Comment Utility
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
 

Author Comment

by:beckyng
Comment Utility
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
Comment Utility
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
Comment Utility
Try:
SELECT * FROM table WHERE CONTAINS(Matcode, ' RPL0001 or RPL0003 ')
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 31

Expert Comment

by:awking00
Comment Utility
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 31

Accepted Solution

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

Author Comment

by:beckyng
Comment Utility
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 31

Expert Comment

by:awking00
Comment Utility
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:ScottPletcher
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
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
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

772 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

11 Experts available now in Live!

Get 1:1 Help Now