Link to home
Start Free TrialLog in
Avatar of andyw27
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.
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

Select * From Table1 Where FiledName Like '%' + 'SerchTest' + '%';

Replace * with field you want to be returned.

Mike
Avatar of andyw27
andyw27

ASKER

Thanks, would this work irrespective of where the words were position?
Avatar of Scott Pletcher
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]%'
Below, I made table Q_28614163_T
CREATE TABLE Q_28614163_T(SerachText VARCHAR(100))

Open in new window

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')

Open in new window


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')

Open in new window


(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
Avatar of andyw27

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
ASKER CERTIFIED SOLUTION
Avatar of Mike Eghtebas
Mike Eghtebas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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:
SELECT *
FROM TableName
WHERE CONTAINS(ColumnName, 'London AND sunny')

Open in new window