?
Solved

TSQL Search on String

Posted on 2010-09-17
6
Medium Priority
?
820 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 668 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 664 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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 668 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

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

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. …
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

762 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