Solved

search for words - any order

Posted on 2015-02-10
7
55 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:Scott Pletcher
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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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 47

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Server Select - find rows created in last 6 months 4 38
best counters for cpu high usage 3 33
How to share SSIS Package? 6 37
Sql Server group by 10 30
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article I will describe the Copy Database Wizard 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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

831 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