Solved

search for words - any order

Posted on 2015-02-10
7
47 Views
Last Modified: 2015-03-12
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.
0
Comment
Question by:andyw27
7 Comments
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40601491
Select * From Table1 Where FiledName Like '%' + 'SerchTest' + '%';

Replace * with field you want to be returned.

Mike
0
 

Author Comment

by:andyw27
ID: 40601641
Thanks, would this work irrespective of where the words were position?
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 40601679
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]%'
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40601742
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
0
 

Author Comment

by:andyw27
ID: 40601823
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
0
 
LVL 33

Accepted Solution

by:
Mike Eghtebas earned 500 total points
ID: 40601837
Then use the solution from Scott.

The option 2 above, give if either is found:

Select * From Q_28614163_T Where SerachText Like '%' + 'London' + '%' OR SerachText Like '%' + 'sunny' + '%'

Make sure the bold fonts match whatever you have,
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 40605391
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

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now