SQL Server Full Text Search Tutorial: CONTAINS, FORMSOF and CONTAINSTABLE

SQL Server 6 min readUpdated Nov 20, 2026
5 views0 comments

The LIKE '%word%' operator reads every row and only matches letters. Full text search keeps a ready made index of words, so SQL Server can find whole words, all the forms of a word, phrases and rank the best matches, quickly, even in long text columns.

Part of the SQL Server for Beginners series. This article is the written version of lesson 19 of the Bigland Network video course. All examples were run in SQL Server Management Studio on a test instance with the Full-Text Search feature installed. Run every example on a test server, not on production.

Watch lesson 19 as a video or open it on YouTube. The written lesson below follows the same steps and the same sample data.


Contents

1. The test table

CREATE TABLE dbo.Articles (
    Id    INT IDENTITY(1,1) CONSTRAINT PK_Articles PRIMARY KEY,
    Title VARCHAR(100) NOT NULL,
    Body  VARCHAR(400) NOT NULL);
INSERT INTO dbo.Articles (Title, Body) VALUES
 ('Backup basics', 'A full backup copies the whole database. Run backups every night and test your restore.'),
 ('Index tuning', 'A good index helps queries run faster. Too many indexes slow down every insert.'),
 ('Query performance', 'Slow queries often need a better index, or fewer rows to read. Check the execution plan.'),
 ('Security for beginners', 'Give users the least permissions they need. Never share the sa login.'),
 ('Running jobs', 'SQL Agent runs jobs on a schedule, like a nightly backup job.'),
 ('Restore drill', 'Practice a restore before the real disaster. A backup you never restored is only a hope.');
SELECT Id, Title FROM dbo.Articles;

A full text index needs a unique, single column, not null key: here the named primary key PK_Articles.

2. The LIKE way

SELECT Id, Title FROM dbo.Articles
WHERE Body LIKE '%backup%';
Id Title
1 Backup basics
5 Running jobs
6 Restore drill

It works, but SQL Server must read the text of every row, it matches letters and not words, and it cannot rank the results.

3. Catalog and full text index

CREATE FULLTEXT CATALOG ft_Articles;
CREATE FULLTEXT INDEX ON dbo.Articles (Title, Body)
    KEY INDEX PK_Articles ON ft_Articles
    WITH CHANGE_TRACKING AUTO;
SELECT c.name AS CatalogName, i.is_enabled
FROM sys.fulltext_indexes i
JOIN sys.fulltext_catalogs c ON c.fulltext_catalog_id = i.fulltext_catalog_id;

Result: one row, ft_Articles, is_enabled = 1. After creation SQL Server fills (populates) the index in the background: on a big table wait for it to finish before you trust the results.

4. CONTAINS: words, word forms, AND, phrases

-- one exact word: articles 1 and 2
SELECT Id, Title FROM dbo.Articles WHERE CONTAINS(Body, 'run');

-- all forms of the word (run, runs, running, ran): articles 1, 2 and 5
SELECT Id, Title FROM dbo.Articles
WHERE CONTAINS(Body, 'FORMSOF(INFLECTIONAL, run)');

-- both words: articles 1 and 6
SELECT Id, Title FROM dbo.Articles WHERE CONTAINS(Body, 'backup AND restore');

-- an exact phrase: article 5
SELECT Id, Title FROM dbo.Articles WHERE CONTAINS(Body, '"nightly backup"');

Use OR when either word is enough, AND NOT to exclude a word, and a star for a prefix, for example '"ind*"'.

5. Ranking with CONTAINSTABLE

SELECT a.Id, a.Title, k.[RANK]
FROM CONTAINSTABLE(dbo.Articles, Body, 'index OR plan') AS k
JOIN dbo.Articles a ON a.Id = k.[KEY]
ORDER BY k.[RANK] DESC;
Id Title RANK
3 Query performance 32
2 Index tuning 24

A higher RANK is a better match. The exact numbers can differ between servers, so trust the order, not the values.

6. A classic error

SELECT Id, Title FROM dbo.Articles
WHERE CONTAINS(Body, 'index backup');

Error 7630: syntax error near ‘backup’ in the full-text search condition. Two words need an operator: 'index AND backup' or 'index OR backup'.

7. Mistakes and best practices

  • Two words with no operator.
  • Searching a column that is not in the full text index.
  • Querying right after creating the index, while it is still filling.
  • Expecting CONTAINS to find part of a word.
  • Using LIKE with a leading percent on a big table.
  • Best practice: use full text for words in long text, index only the columns people search, use CHANGE_TRACKING AUTO, use CONTAINSTABLE when the order of the best match matters, and keep LIKE for small, simple searches.

8. Practice and clean up

  • Find the articles that mention restore in Title or Body.
  • Which articles mention backup but not restore?
  • How many articles mention backup in Title or Body?
-- 1   (articles 1 and 6)
SELECT Id, Title FROM dbo.Articles WHERE CONTAINS((Title, Body), 'restore');

-- 2   (article 5)
SELECT Id, Title FROM dbo.Articles WHERE CONTAINS(Body, 'backup AND NOT restore');

-- 3   (3)
SELECT COUNT(*) AS Articles_With_Backup FROM dbo.Articles WHERE CONTAINS((Title, Body), 'backup');

-- clean up: index, then catalog, then table
DROP FULLTEXT INDEX ON dbo.Articles;
DROP FULLTEXT CATALOG ft_Articles;
DROP TABLE dbo.Articles;

Frequently asked questions

Is full text search installed by default?

It is an optional feature of SQL Server. Check with SELECT SERVERPROPERTY('IsFullTextInstalled'): 1 means it is installed.

Why does my new index return no rows?

The index is filled in the background (population). Wait until it finishes, then query again.

Can CONTAINS find part of a word?

Only from the start of a word, with a prefix term such as '"back*"'. For text in the middle of a word, use LIKE.

What to learn next

The next lesson, lesson 20: Query Store and Slow Queries, covers Query Store, the black box recorder of SQL Server, to find slow queries after the fact. Previous lesson: Lesson 18: Partitioning and Large Tables.

The whole series

Sources and further reading

  • Microsoft Learn, Transact-SQL reference (Database Engine), on learn.microsoft.com: the official syntax for the statements used here.
  • Microsoft Learn, full text search, CONTAINS, FORMSOF and CONTAINSTABLE, for the full details of each feature.
Did you find this helpful?Tap the heart to let us know. It takes one second and no account.

More tutorials like this

Leave a Comment

Your email address will not be published. Required fields are marked *

Bigland Network IT

Practical tutorials for people who run, build and fix things: operating systems, databases, development, networking and security.

Topics

More

Site

© 2026 Bigland Network. All rights reserved.Always test commands on a non-production system first.
Scroll to Top