What is CONTAINS in SQL?

Deepsandhya Shukla Last Updated : 03 Jun, 2024
4 min read

Introduction

In SQL and database management, efficiently querying and retrieving data is paramount. Among the various tools and functions available, the CONTAINS function stands out for its capability to perform full-text searches within text columns. Unlike basic string functions, CONTAINS enables complex queries and patterns, making it a powerful asset for developers and database administrators. This article explores the CONTAINS function in SQL, detailing its syntax, implementation, and practical applications across various industries, from e-commerce to healthcare and education.

What is the CONTAINS Function in SQL?

The CONTAINS function in SQL is a powerful tool for full-text searches. It enables us to search for a specific word or phrase within a text column. Unlike basic string functions, CONTAINS can handle complex queries and text patterns.

Syntax of the CONTAINS Function

The syntax of the CONTAINS function is straightforward. It requires the column name and the search term.

code SELECT * FROM table_name WHERE CONTAINS(column_name, 'search_term');

Key components include:

  • column_name: The column where the search will be performed. This column must be full-text indexed.
  • search_term: The specific word or phrase to search for within the column.

The CONTAINS function returns rows where the search term is found. It performs a boolean evaluation, returning TRUE if the term is found and FALSE otherwise.

You must set up full-text indexing to use the CONTAINS function, allowing efficient text searching.

Prerequisites for Using CONTAINS

Before using CONTAINS, ensure your database supports full-text indexing and that the necessary services are running.

Steps to Create a Full-text Index

Step 1: Create a full-text catalog: This is a storage for the index.

CREATE FULLTEXT CATALOG MyFullTextCatalog;

Step 2: Create a full-text index on a table. Specify the table and columns to index.

CREATE FULLTEXT INDEX ON Products(productName)

KEY INDEX PK_ProductID

ON MyFullTextCatalog;

This setup enables you to perform full-text searches using the CONTAINS function.

Implementing SQL CONTAINS

Basic Usage of CONTAINS

You can use the CONTAINS function once your full-text index is set up. Use a basic query to search for a specific word in a column.

SELECT * FROM Products WHERE CONTAINS(productName, 'bike');

This query returns all rows where productName contains the word “bike.”

If the column is indexed, CONTAINS can search various data types like varchar, text, and ‘xml’

Advanced Search Techniques with CONTAINS

The CONTAINS function supports advanced search techniques for more refined results. For example, you can search for words that start with a specific prefix.

SELECT * FROM Products WHERE CONTAINS(productName, 'bike*');

This query finds all product names starting with “bike”.

Proximity Searches

Find words that appear close to each other in a text.

SELECT * FROM Articles WHERE CONTAINS(content, ‘NEAR((climate, change), 5)’);

This searches for “climate” and “change” within five words of each other.

Synonym Searches

Search for synonyms using a thesaurus.

SELECT * FROM Documents WHERE CONTAINS(content, 'FORMSOF(THESAURUS, "happy")');

This finds documents with words related to “happy”.

These techniques make the CONTAINS function a powerful tool for comprehensive text searches.

Comparing CONTAINS with LIKE

Differences in Functionality

The CONTAINS and LIKE functions serve different purposes in SQL text searching. LIKE is a pattern-matching operator that uses wildcards to find simple text matches. Conversely, CONTAINS is used for full-text searches and supports complex queries, including synonyms and proximity searches.

For example, using LIKE:

SELECT * FROM Products WHERE productName LIKE '%apple%';

Using CONTAINS:

SELECT * FROM Products WHERE CONTAINS(productName, 'apple');

Performance Considerations

LIKE is simpler and faster for small datasets. It does not require special indexing. However, CONTAINS is more efficient for large datasets. It requires a full-text index but offers faster search results due to its indexing.

Use Cases

Using LIKE for straightforward pattern matching, such as:

SELECT * FROM Users WHERE username LIKE 'john%';

Use CONTAINS for advanced searches, like:

SELECT * FROM Articles WHERE CONTAINS(content, 'FORMSOF(THESAURUS, "happy")');

Real World Examples

Here are a few real-world applications of SQL CONTAINS:

For a retail database, find products with a specific keyword:

SELECT * FROM Products WHERE CONTAINS(productName, 'mountain bike');

For a news database, find articles mentioning two keywords near each other:

SELECT * FROM Articles WHERE CONTAINS(content, 'NEAR((economy, growth), 5)');

Applications in Different Industries

E-commerce

In the e-commerce industry, businesses often have large databases of product descriptions. Customers must find products quickly and efficiently by entering keywords or phrases into a search bar.

Example Use Case: An online retailer uses SQL CONTAINS to enable customers to search for products based on keywords in the product descriptions. For instance, if a customer searches for “waterproof hiking boots,” the system can use the CONTAINS function to return all products with descriptions that include these keywords.

SELECT * FROM products
WHERE CONTAINS(description, ‘waterproof AND hiking AND boots’);

Healthcare

Maintaining comprehensive medical records is crucial in healthcare. Healthcare providers must search patient records for specific symptoms, diagnoses, or treatments.

Example Use Case: A hospital’s database system allows doctors to use SQL CONTAINS to search patient records for symptoms such as “chest pain” or “shortness of breath.” This helps quickly identify patients with similar conditions and review relevant historical data for diagnosis and treatment planning.

Education

In the education sector, researchers and students often need to search vast libraries of academic papers and publications for information on specific topics.

Example Use Case: A university’s digital library system employs SQL CONTAINS to enable students and researchers to search for academic papers that discuss topics such as “machine learning” or “climate change.” This function helps users locate relevant research materials efficiently.

Conclusion

The CONTAINS function in SQL is essential for advanced full-text searches, surpassing the LIKE operator in capability and efficiency for large datasets. Setting up full-text indexing is crucial for its use. Mastering techniques like word prefixes, proximity searches, and synonyms enhance data retrieval. Alternatives like CHARINDEX, PATINDEX, and STRING_SPLIT with IN offer additional text-searching options. These methods are valuable across various industries, from e-commerce to healthcare. 

Responses From Readers

Clear

Susan Andrew
Susan Andrew

How can the relevance ranking provided by the CONTAINS function be leveraged to improve search results?

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details