Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get leading wildcard full-text searches to work in SQL Server?

Note: I am using SQL's Full-text search capabilities, CONTAINS clauses and all - the * is the wildcard in full-text, % is for LIKE clauses only.

I've read in several places now that "leading wildcard" searches (e.g. using "*overflow" to match "stackoverflow") is not supported in MS SQL. I'm considering using a CLR function to add regex matching, but I'm curious to see what other solutions people might have.

More Info: You can add the asterisk only at the end of the word or phrase. - along with my empirical experience: When matching "myvalue", "my*" works, but "(asterisk)value" returns no match, when doing a query as simple as:

SELECT * FROM TABLENAME WHERE CONTAINS(TextColumn, '"*searchterm"'); 

Thus, my need for a workaround. I'm only using search in my site on an actual search page - so it needs to work basically the same way that Google works (in the eyes on a Joe Sixpack-type user). Not nearly as complicated, but this sort of match really shouldn't fail.

like image 729
Greg Hurlman Avatar asked Aug 06 '08 13:08

Greg Hurlman


People also ask

How does full text search work in SQL Server?

Full-text queries perform linguistic searches against text data in full-text indexes by operating on words and phrases based on the rules of a particular language such as English or Japanese. Full-text queries can include simple words and phrases or multiple forms of a word or phrase.

How do I enable full-text index in SQL Server?

Expand Tables, and right-click the table that you want to disable or re-enable for full-text indexing. Select Full-Text index, and then click Disable Full-Text index or Enable Full-Text index.

How do I add full text search to existing SQL Server?

Under 'Instance Features', select 'Full-Text and Semantic Extractions for Search'. Click [Next]. Accept the default settings for Server Configuration. Click [Next].


1 Answers

Workaround only for leading wildcard:

  • store the text reversed in a different field (or in materialised view)
  • create a full text index on this column
  • find the reversed text with an *

    SELECT *  FROM TABLENAME  WHERE CONTAINS(TextColumnREV, '"mrethcraes*"'); 

Of course there are many drawbacks, just for quick workaround...

Not to mention CONTAINSTABLE...

like image 145
xnagyg Avatar answered Oct 10 '22 22:10

xnagyg