andyw27
asked on
search for words - any order
Hi,
Using SQL 2008 (without indexing) I need to be able to search a column for any number of words no matter which order they are found. For example terrm1 term2 should return the same as term2 term1
Thanks.
Using SQL 2008 (without indexing) I need to be able to search a column for any number of words no matter which order they are found. For example terrm1 term2 should return the same as term2 term1
Thanks.
ASKER
Thanks, would this work irrespective of where the words were position?
This would find those strings of letters in any order:
...
WHERE
column LIKE '%term1%' AND
column LIKE '%term2%'
But, do you specifically mean entire "word"? If so, you'd want to make sure there was not an alphabetic character around the terms.
WHERE
SPACE(1) + column + SPACE(1) LIKE '%[^a-z]term1[^a-z]%' AND
SPACE(1) + column + SPACE(1) LIKE '%[^a-z]term2[^a-z]%'
...
WHERE
column LIKE '%term1%' AND
column LIKE '%term2%'
But, do you specifically mean entire "word"? If so, you'd want to make sure there was not an alphabetic character around the terms.
WHERE
SPACE(1) + column + SPACE(1) LIKE '%[^a-z]term1[^a-z]%' AND
SPACE(1) + column + SPACE(1) LIKE '%[^a-z]term2[^a-z]%'
Below, I made table Q_28614163_T
Option 1: You want hard code a single value like 'MTV' to search for, then use:
Select * From Q_28614163_T Where SerachText Like '%' + 'MTV' + '%';
Option 2: You want to hard code two values like 'MTV' and 'ABC' to search for, use:
Select * From Q_28614163_T Where SerachText Like '%' + 'MTV' + '%' OR SerachText Like '%' + 'ABC' + '%'
Option 3: You want to enter a list of search criterias in a table like:
(Revised:) For the SELECT clause to find the listed values in table Q_28614163 and return their records?
If option 1 or option 2 are acceptable, then use their listed solution. But if you want the 3rd option, it has to be worked at.
Mike
CREATE TABLE Q_28614163_T(SerachText VARCHAR(100))
And inserted some rows to be searched:INSERT Q_28614163_T (SerachText) VALUES
(N'The ABC news anchor Brain Willams is interviewd by MTV For his...')
,(N'The new host of the late night show CBS will be who worked in Commdey Central ')
,(N'The MTV channel was vwey helpfull to movie industry because')
,(N'TLC')
Option 1: You want hard code a single value like 'MTV' to search for, then use:
Select * From Q_28614163_T Where SerachText Like '%' + 'MTV' + '%';
Option 2: You want to hard code two values like 'MTV' and 'ABC' to search for, use:
Select * From Q_28614163_T Where SerachText Like '%' + 'MTV' + '%' OR SerachText Like '%' + 'ABC' + '%'
Option 3: You want to enter a list of search criterias in a table like:
CREATE TABLE Q_28614163(SerachWord VARCHAR(50))
GO
INSERT Q_28614163 (SerachWord) VALUES
('ABC')
,('CBS')
,('MTV')
,('TLC')
(Revised:) For the SELECT clause to find the listed values in table Q_28614163 and return their records?
If option 1 or option 2 are acceptable, then use their listed solution. But if you want the 3rd option, it has to be worked at.
Mike
ASKER
Take this example, 3 rows of data:
London is very sunny today
This time of year London can be sunny
England is sunny this time of year
If the user provides the following search string:
London sunny
I would expect it to return the first two rows, likewise if the search string was sunny London
London is very sunny today
This time of year London can be sunny
England is sunny this time of year
If the user provides the following search string:
London sunny
I would expect it to return the first two rows, likewise if the search string was sunny London
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Did you think about Full Text Search as solution for this?
With Full Text Search you'll have a search functionality more similar to an Internet Search Engine. For your case you could use something like:
With Full Text Search you'll have a search functionality more similar to an Internet Search Engine. For your case you could use something like:
SELECT *
FROM TableName
WHERE CONTAINS(ColumnName, 'London AND sunny')
Replace * with field you want to be returned.
Mike