SQL CONTAINS Function Vs LIKE Operator

Despite the fact that LIKE is an operator and CONTAINS is a predicate, the two can be used interchangeably in a variety of situations.

We'll look at how to utilise LIKE and CONTAINS to find a word or phrase, as well as the key differences between the two.


SQL CONTAINS Function

CONTAINS is a strong search tool that employs a context index to create a word tree that can be searched using the CONTAIN search syntax.

It can be used to search for a single word or a group of words, and it has its own syntax, which includes boolean operators.

It's also more powerful since it provides a "score" instead of just "matched" or "not matched," which can be used to rank results in order of significance;

When using fuzzy logic to search, it can find:

  • a single word or sentence
  • A word's or phrase's prefix.
  • A term that is close to another word.

When looking for the word 'go,' for example, you'll see goes, going, went, and so on.

For example, if you search for farm animal, you'll see cow, horse, and other similar terms.

Example: This query looks for products that have the word 're' in the package description.

SELECT CustomerName, Packagedetails
  FROM Product 
 WHERE CONTAINS(Package, 're')

SQL LIKE Operator

LIKE is a very basic string pattern matcher that recognises two wildcards (percent) and (_), which match zero or more characters and exactly one character, respectively.

In your situation, percent a percent e percent looks for zero or more characters followed by a, then zero or more characters followed by e, then zero or more characters.

It also has a very straightforward return value: "matched" or "not matched," with no shades of grey in between.

Example: Like Operator

SELECT *
FROM Book
WHERE BookName LIKE 'SQL';

SQL Difference Between CONTAIN Function And LIKE Operator

There are 7 main contrast in contain vs like:-

CONTAIN LIKE
CONTAINS is a predicate that can be used to find a word, a word's prefix, a word near another word, a word's synonym, and so on. The LIKE operator is used to determine whether a character string matches a pattern.
Full-text search is limited to full-text indexed columns. It is not necessary to index the column.
Asterisk (*) is the sole wildcard character that can be used. In a pattern, you can still use standard characters or wildcard characters like percent, _, [], and [].
The wildcard asterisk (*) only works if it is at the end of a word or phrase. '"Some Phrase*"' is an example. The wildcard characters can be used as needed throughout the pattern. 'Some P[a-g] percent ', '_ome Phrase', '[P-S]ome Phrase', 'Some P[a-g] percent ', and so on.
Because wildcards can only be used at the end, performance should be faster because indexes such as the full-text index can be used. If you use wildcards at the start of the pattern, it may not be able to use all of the indexes, therefore performance may be slower.
nvarchar is the search condition. Large strings, such as varcher(max) and nvarchar(max), aren't allowed. The pattern for comparison is a string with a maximum length of 8,000 bytes.
It's also more powerful in that it produces a "score" rather than a basic "matched" or "not matched" result. It also has a very straightforward return value: "matched" or "not matched" - no grey area.