Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 402
  • Last Modified:

MSSql search data with "LIKE" from list of items

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
beckyng
Asked:
beckyng
  • 3
  • 3
  • 2
  • +4
1 Solution
 
Dale BurrellCommented:
where MatCode like '%RPL0001%' or MatCode like '%RPL0003%'

Open in new window

0
 
beckyngAuthor Commented:
Sorry, May I ask if there are over 100 items from the list, how should I do?

Thanks
0
 
Dale BurrellCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
beckyngAuthor Commented:
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
 
Louis01Commented:
select *
  from myTable t1
 where exists (select 1 
                 from myListTable t2 
                where t1.MatCode like '%' + t2.myCol + '%')

Open in new window

0
 
13ShadowCommented:
Try:
SELECT * FROM table WHERE CONTAINS(Matcode, ' RPL0001 or RPL0003 ')
0
 
awking00Commented:
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
 
awking00Commented:
select * from matcode as m
inner join searchcode as s
on charindex(s.search_val,m.code) > 0;
0
 
beckyngAuthor Commented:
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
 
awking00Commented:
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
 
Scott PletcherSenior DBACommented:
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
 
PortletPaulCommented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now