Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

search for words - any order

Posted on 2015-02-10
7
Medium Priority
?
62 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 70

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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 52

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

636 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