Solved

TSQL Search on String

Posted on 2010-09-17
6
765 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
6 Comments
 
LVL 142

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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now