How Can I Select All Rows Where Column Contain Any Words Of A String
I started by asking this question I wish to Select all rows which contain any element from an array And while my current question is almost identical, it serves a whole different p
Solution 1:
one way in sql server is
push the word in temp table like this
DECLARE @SearchWords TABLE (
word varchar(30) )
INSERT INTO @SearchWords
(word)
VALUES ('Jack')
,('Pontiac')
,('Bloggs');
than join this table with the actual table
SELECT a.* FROM table_data a
INNER JOIN @SearchWords b on a.record_desc like '%' + b.word + '%'
Solution 2:
You could try something like this:
SELECT * FROM table WHERE name LIKE '%word1%' OR name LIKE '%word2%';
The %
signs are the analogous to *
in typical searches.
To take the string and create this query for multiple words you could do something like this:
String inputString = "hello my friend";
String[] wordlist = inputString.split(" ");
String query = "SELECT * FROM table WHERE 0=0 ";
for(String word : wordlist) {
query += "OR name LIKE '%" + word + "%' ";
}
The spaces at the end of the strings are important, don't forget those! Also, you'll probably want to sanitize the string before you split it (remove extra punctuation, maybe even remove words like "a" or "the" or "to" etc.)
Post a Comment for "How Can I Select All Rows Where Column Contain Any Words Of A String"