Solved

TSQL Search on String

Posted on 2010-09-17
6
813 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

688 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