Solved

Using FREETEXT SEARCH to do Partial Match

Posted on 2007-11-14
8
814 Views
Last Modified: 2010-08-05
Hi there I am using FREETEXTSEARCH, Here are the clauses in question.

AND
((@Search is null or FREETEXT(DescriptionSelConcept, @Search) )
or
(@Search is null or FREETEXT(LessonsLearnt, @Search) )
or
(@Search is null or FREETEXT(ProjectTitle, @Search) )
or
(@Search is null or FREETEXT(BriefDescriptionScope, @Search) )
or
(@Search is null or FREETEXT(JobNumber, @Search) ))

At the moment these clauses only do exact match for instance if im searching for "blackhorse" in the ProjectTitle I MUST type in Blackhorse for the result to appear, I would like to be able to type "black" as well for it to appear, does anyone know how I can modify this to do this?
0
Comment
Question by:MayoorPatel
  • 3
  • 3
  • 2
8 Comments
 
LVL 42

Expert Comment

by:EugeneZ
ID: 20280653
try to use CONTAINS instead of FREETEXT:
http://msdn2.microsoft.com/en-us/library/aa258227(SQL.80).aspx
----------------------------------------
Example:

USE Northwind
GO
SELECT ProductName
FROM Products
WHERE CONTAINS(ProductName, ' "choc*" ')
GO
0
 
LVL 1

Author Comment

by:MayoorPatel
ID: 20280975
Nope just tried this

AND
((@Search is null or CONTAINS(DescriptionSelConcept, @Search) )
or
(@Search is null or CONTAINS(LessonsLearnt, @Search) )
or
(@Search is null or CONTAINS(ProjectTitle, @Search) )
or
(@Search is null or CONTAINS(BriefDescriptionScope, @Search) )
or

and its still not returning the record on partial match
0
 
LVL 42

Expert Comment

by:EugeneZ
ID: 20281198
try rtrim(@search) + '*'
0
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.

 
LVL 1

Author Comment

by:MayoorPatel
ID: 20281226
AND
((@Search is null or CONTAINS(DescriptionSelConcept, rtrim(@search) + '*') )
or
(@Search is null or CONTAINS(LessonsLearnt, rtrim(@search) + '*') )
or
(@Search is null or CONTAINS(ProjectTitle, rtrim(@search) + '*') )
or
(@Search is null or CONTAINS(BriefDescriptionScope, rtrim(@search) + '*') )
or


gives a syntax error in the SQL


0
 
LVL 42

Expert Comment

by:EugeneZ
ID: 20282204
try

--
AND
((@Search is null or CONTAINS(DescriptionSelConcept,'"'+@search+'*"') )
or
(@Search is null or CONTAINS(LessonsLearnt, '"'+@search+'*"') )
or
(@Search is null or CONTAINS(ProjectTitle, '"'+@search+'*"' )
or
(@Search is null or CONTAINS(BriefDescriptionScope,'"'+@search+'*"') )
or

----------
see working example:


USE AdventureWorks;
GO
declare @search varchar(50)
set @search=' Chain '
set @search='"'+@search+'*"'
SELECT Name
FROM Production.Product
WHERE CONTAINS(Name, @search);
GO
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 20282377
You might find it a little more efficient, if you set the @Search before and use * instead of repeating all the columns in endless OR clauses, as in:

SET @Search = '"' + @Search + '*"'

...

AND
@Search Is Null Or CONTAINS(*, @Search)

0
 
LVL 1

Author Comment

by:MayoorPatel
ID: 20287777
aceperkins - Yes that would be correct IF I had wanted to search every column in the table as I only need to search 4 of them the way I have done it is correct.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 20288746
No problem.  On a different note, please attend to all your abandoned questions:
1 10/19/2007 500 Converting a Dynamic versio... Open ASP
2 10/08/2007 500 Building A Search Query Open MS SQL Server ...
3 09/14/2007 500 How to Search 2 Colums in a... Open MS SQL Server
4 07/04/2007 500 UNION with DYNAMIC ORDER BY... Open Databases ...
5 07/04/2007 500 Filtering a Datalist Orderi... Open ASP.Net Programm...
6 06/25/2007 500 Overlapping Rendering of El... Open Firefox ...
7 06/22/2007 500 Object reference not set to... Open ASP.Net Programm... ...
8 04/04/2007 500 Group By Clause Problem wit... Open SQL Server 2005 ...
9 03/27/2007 500 How to Avoid Divide by Zero... Open MS SQL Server
10 03/26/2007 500 Calculated Column to calcul... Open SQL Server 2005 ...

Thanks.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

809 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