Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Problem: Using CONTAINS() doesn't work, but LIKE works fine

I have a Products table in a SQL Server database and I am having to troubleshoot a legacy stored procedure that uses Full-Text indexing. For our purposes here, lets suppose that the Products table has two fields ID, Keywords. And the Keywords field is populated with the following:

ROLAND SA-300 This Roland SA-300 is in MINT condition!

When I run the following statement, I am able to retrieve the record:

SELECT * FROM Products WHERE Keywords LIKE '%SA-300%'

However, when I run any of the following statements I get zero results:

SELECT * FROM Products WHERE CONTAINS(Keywords, ' SA-300 ')
SELECT * FROM Products WHERE CONTAINS(Keywords, 'SA-300')
SELECT * FROM Products WHERE CONTAINS(Keywords, '"SA-300"')
SELECT * FROM Products WHERE CONTAINS(Keywords, '"*SA-300*"')

But I know that the CONTAINS() function is working because I get results when I run any of these:

SELECT * FROM Products WHERE CONTAINS(Keywords, ' Roland ')
SELECT * FROM Products WHERE CONTAINS(Keywords, 'Roland')
SELECT * FROM Products WHERE CONTAINS(Keywords, '"Roland"')
SELECT * FROM Products WHERE CONTAINS(Keywords, '"*Roland*"')
SELECT * FROM Products WHERE CONTAINS(Keywords, 'Roland')

I need to figure out why the CONTAINS() function isn't working on the 'SA-300' term. I am new to Full-text indexes, so any help is appreciated.

like image 572
jessegavin Avatar asked Feb 25 '09 21:02

jessegavin


People also ask

What is difference between contains and like in SQL?

The 'Contains' operator adds a wild character automatically to the search string, while the 'Like' operator matches for the exact search string.

Can you do contains in SQL?

CONTAINS is a predicate used in the WHERE clause of a Transact-SQL SELECT statement to perform SQL Server full-text search on full-text indexed columns containing character-based data types. CONTAINS can search for: A word or phrase. The prefix of a word or phrase.

Does not contain operator in SQL?

The SQL NOT condition (sometimes called the NOT Operator) is used to negate a condition in the WHERE clause of a SELECT, INSERT, UPDATE, or DELETE statement.

Does not contain like in SQL?

The NOT LIKE operator in SQL is used on a column which is of type varchar . Usually, it is used with % which is used to represent any string value, including the null character \0 . The string we pass on to this operator is not case-sensitive.


1 Answers

Two thoughts:

(1) The hyphen might be treated as a word break What do these return?

SELECT * FROM Products WHERE CONTAINS(Keywords, 'SA')
SELECT * FROM Products WHERE CONTAINS(Keywords, 'SA 300')
SELECT * FROM Products WHERE CONTAINS(Keywords, 'SA300')

See this other question.

(2) Have you tried rebuilding your full-text index? It is possible it is out of date.

like image 62
JohnFx Avatar answered Sep 19 '22 14:09

JohnFx