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.
The 'Contains' operator adds a wild character automatically to the search string, while the 'Like' operator matches for the exact search string.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With