Dynamic similar expression in SQL

I have brains and brains how to do this for a while, and I know that some genius on this site will have an answer. I am basically trying to do this:

SELECT column FROM table WHERE [table].[column] LIKE string1 OR [table].[column] LIKE string2 OR [table].[column] LIKE string3... 

for a list of search strings stored in a table column. Obviously, I cannot do an operator like for each row manually, because I want the table to be dynamic.

Any suggestions would be great .: D

EDIT:

I am using MSSQL: (

+6
sql sql-server sql-like
source share
3 answers

Put the parameters (string1, string2, string3 ...) in the table ( Params ), then JOIN in the table using the LIKE JOIN clause, for example.

 SELECT column FROM table AS T1 INNER JOIN Params AS P1 ON T1.column LIKE '%' + P1.param + '%'; 
+17
source share

Make a sample Table_1:

 id Name 1 Fred 2 Joe 3 Frederick 4 Joseph 5 Kenneth 

To find all the Freds and Jos you encode

 SELECT * FROM Table_1 WHERE name like 'Fred%' OR name like 'Jo%' 

What would you like is a dynamic WHERE. You can achieve this by setting wildcards in Table_2:

 id Search 1 Fred% 2 Jo% 

and doing LIKE with INNER JOIN:

 SELECT * FROM Table_1 INNER JOIN Table_2 ON Table_1.name LIKE Table_2.search 

Result:

 id Name id Search 1 Fred 1 Fred% 3 Frederick 1 Fred% 2 Joe 2 Jo% 4 Joseph 2 Jo% 
+1
source share

It looks like you are trying to do a full-text search. MySQL has a tool for this:
http://dev.mysql.com/doc/refman/5.0/en/fulltext-boolean.html

 SELECT column FROM table MATCH (column) AGAINST ('string1 string2 string3' IN BOOLEAN MODE) 

In the case of MSSQL, some information is available at http://msdn.microsoft.com/en-us/library/cc879300.aspx

 SELECT column FROM table WHERE CONTAINS (column , 'string1 string2 string3'); 
0
source share

All Articles