Solved

search for words - any order

Posted on 2015-02-10
7
57 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 34

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
Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

 
LVL 34

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 34

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 49

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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Job Hung 17 37
Get data from two MySQL tables 6 27
Insert multiple records into a table 4 14
Find Patients last appointment location 4 16
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

733 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