I’ve been working on a new presentation for full text search and brushing up on some of my T-SQL operators. Part of my talk goes into the CONTAINS operator, which is one of the full text search keywords you need to know.
This operator is only used with full text indexes, so if you have a column that isn’t full-text indexed, it returns an error. If I issue this:
SELECT * FROM dbo.salary WHERE CONTAINS(empname, 'Steve')
I get this:
Msg 7601, Level 16, State 2, Line 3
Cannot use a CONTAINS or FREETEXT predicate on table or indexed view ‘dbo.salary’ because it is not full-text indexed.
I have a table that is full text indexed and I can issue a basic query, which looks like so many other T-SQL queries.
SELECT name FROM authordrafts WHERE CONTAINS(*, 'AlwaysOn') ; go
This returns me all the rows where the columns in the full-text index (I used the star, *), have the term “AlwaysOn” in them. In this case, I’m hitting a FileTable table with lots of whitepapers in there.
This query is essentially a LIKE search, but it isn’t doing character matching. Instead it is working with those keywords in the full text index. I’ve used a simple search above. I could replace the * with the column, in this case the file_stream column.
SELECT name FROM authordrafts WHERE CONTAINS(file_stream, 'AlwaysOn') ; go
I could also use a prefix term and the * wildcard, similar to LIKE.
SELECT name FROM authordrafts WHERE CONTAINS(file_stream, 'Always*') ; go
These match other rows where “always” is the document, which matches “always” as a standalone word as well as “alwayson” as a term.
I could also limit the search to particular columns, using parenthesis and commas to separate them out. The BOL example from the CONTAINS page does a nice job of showing this.
Use AdventureWorks2012; GO SELECT Name, Color FROM Production.Product WHERE CONTAINS((Name, Color), 'Red');
This is just a very basic look at CONTAINS. In another post, I’ll look at a few more possibilities with this term.
hi
during my testing or learning on Full text search , i found below queries are returning different results .can you please guide me why
select [BusinessEntityID] , [Name] from [Person].[Person_BK]
where contains([Name], ‘*ASDGDE*’) or contains([Name], ‘*AFASFA*’)
select [BusinessEntityID] , [Name] from [Person].[Person_BK]
where [Name] like ‘%ASDGDE%’ or [Name] like ‘%AFASFA%’
LikeLike
i got the correct query select * from [Person].[Person_BK] where
contains([Name], ‘*ASDGDE* OR *AFASFA*’)
LikeLike
I’m not entirely sure what your differences or are what you are looking for. LIKE works differently than CONTAINS. If you post a more detailed question with what your results are at http://www.sqlservercentral.com/forums with your question, we’ll get you an answer.
LikeLike