Solved

TSQL Search on String

Posted on 2010-09-17
6
806 Views
Last Modified: 2012-05-10
Hi Experts,

I would like to create a stored proc that would search on the exact term keyed in (could be one word or phrase and then search on the seperate words in the phase:

Sample Search "Experts Exchange"
WHERE = like 'experts exchange' OR like 'experts' OR like 'exchange'

What is the best practice for this (speed etc)? Im going to take out all stop words so I just get a search on only the keywords typed.

Regs,
Numb
0
Comment
Question by:ComfortablyNumb
[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
6 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 167 total points
ID: 33700903
this sounds like a FULL TEXT index candidate:
http://msdn.microsoft.com/en-us/library/ms187317.aspx

once you create and ensure the full text catalog is populated/updated regularly, you can use the CONTAINS, CONTAINSTABLE,  FREETEXT or FREETEXTTABLE methods
http://technet.microsoft.com/en-us/library/ms187787.aspx
0
 
LVL 7

Assisted Solution

by:tlovie
tlovie earned 166 total points
ID: 33700906
Sounds like a job for full text indexing.

Have a look at this... it's not trivial to set up, but the other options for doing the search as you describe, may end up lacking in robustness.

http://www.developer.com/db/article.php/3446891/Understanding-SQL-Server-Full-Text-Indexing.htm
0
 

Author Comment

by:ComfortablyNumb
ID: 33701083
You would think so. But unfortuately my live version of SQL Server hasn't got it installed (Standard Edition SP3) and our hosting company says its not part of the dedicated hosting package. This is despite paying near £500 per month (approx $850) for it!

So I kinda need some kind of split function or something. However, I am going to get into a heavy discussion with our hosts as well!!!

0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 7

Expert Comment

by:tlovie
ID: 33701429
Standard edition comes with Full Text indexing, I can't remember if the default install options have it turned on, however, you just need to install it and turn it on.  I don't have much experience in it, but from what I read, it looks easy to configure, and quite robust.
0
 
LVL 23

Assisted Solution

by:Rajkumar Gs
Rajkumar Gs earned 167 total points
ID: 33701438
Without FULL TEXT INDEXING, search would be very slow.

Try this code which is an alternative, but not recommended on table contains more data

Raj
CREATE TABLE #table
(
	Name	VARCHAR(20)
)
INSERT INTO #table
	SELECT 'Experts Exchange' UNION
	SELECT 'Experts' UNION
	SELECT 'Exchange' UNION
	SELECT 'Experts Suggestion' UNION
	SELECT 'Exchange Offer' UNION
	SELECT 'Expert' UNION
	SELECT 'Subscription' UNION
	SELECT 'Account' UNION
	SELECT 'Question'

-- 9 records
select * from #table 

-- 6 matched records
SELECT a.* FROM #table a
	INNER JOIN (SELECT * FROM dbo.Split('Experts Exchange', ' ')) b
		on CHARINDEX(b.items, a.[Name]) > 0

DROP TABLE #table

Open in new window

0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 33701454
Here is the split function used .
Raj
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


create FUNCTION [dbo].[Split](@String varchar(8000), @Delimiter char(1))        
returns @temptable TABLE (items varchar(8000))        
as        
begin        
    declare @idx int        
    declare @slice varchar(8000)        
       
    select @idx = 1        
        if len(@String)<1 or @String is null  return        
       
    while @idx!= 0        
    begin        
        set @idx = charindex(@Delimiter,@String)        
        if @idx!=0        
            set @slice = left(@String,@idx - 1)        
        else        
            set @slice = @String        
           
        if(len(@slice)>0)   
            insert into @temptable(Items) values(@slice)        
  
       set @String = right(@String,len(@String) - @idx)        
        if len(@String) = 0 break        
    end    
return        
end

Open in new window

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Finding gaps or missing dates in a date range using TSQL 3 38
sql query display the latest row 10 54
How come this XML node is not read? 3 32
denied execute as 13 33
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

696 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