Table of Contents

Introduction

SQL Server LIKE operator is used to search for a specific pattern in a character string. It allows you to filter records based on partial text matches instead of requiring an exact value. The LIKE operator is commonly used with the WHERE clause to find names, email addresses, product details, and other text-based data.

For example, if you want to find all employees whose names start with the letter A, you can use the LIKE operator with the % wildcard. Similarly, you can search for words that end with a particular character or contain a specific sequence of characters.

SQL Server supports several wildcard characters with the LIKE operator, including %, _, and character lists. These wildcards make text searching flexible and useful in database applications.

SQL Server LIKE Operator Syntax

The basic syntax of the SQL Server LIKE operator is:

SELECT column1, column2, ...
FROM table_name
WHERE column_name LIKE pattern;

Here:

  • SELECT specifies the columns you want to retrieve.
  • FROM specifies the table containing the data.
  • WHERE filters records based on a condition.
  • LIKE checks whether a string matches the specified pattern.
  • pattern defines the text pattern you want to search for.

You can also use the NOT LIKE operator to find records that do not match a particular pattern.

Sample Employees Table

Before exploring the LIKE operator, let’s create a sample table named Employees.

CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY,
    FirstName VARCHAR(50),
    LastName VARCHAR(50),
    Email VARCHAR(100),
    Department VARCHAR(50),
    Salary DECIMAL(10, 2)
);

Insert some sample records into the table:

INSERT INTO Employees
(EmployeeID, FirstName, LastName, Email, Department, Salary)
VALUES
(1, 'John', 'Smith', 'john@example.com', 'IT', 50000),
(2, 'Alice', 'Brown', 'alice@example.com', 'HR', 45000),
(3, 'Michael', 'Johnson', 'michael@example.com', 'IT', 60000),
(4, 'David', 'Wilson', 'david@example.com', 'Finance', 55000),
(5, 'Anna', 'Taylor', 'anna@example.com', 'Marketing', 48000),
(6, 'James', 'Anderson', 'james@example.com', 'IT', 52000),
(7, 'Sarah', 'Miller', 'sarah@example.com', 'HR', 47000),
(8, 'Robert', 'Davis', 'robert@example.com', 'Finance', 58000);

The Employees table now contains sample employee information that will be used in the following examples.

SQL Server LIKE with Percentage (%) Wildcard

The percentage (%) wildcard represents zero, one, or multiple characters. It is one of the most commonly used wildcards with the LIKE operator.

You can place % at the beginning, end, or both sides of a search pattern.

1. Find Values That Start With a Specific Character

To find employees whose first names start with the letter A, use the following query:

SELECT *
FROM Employees
WHERE FirstName LIKE 'A%';

The query returns employees whose first names begin with A.

Example result:

EmployeeID FirstName LastName
2 Alice Brown
5 Anna Taylor

The pattern 'A%' matches any string that starts with A, followed by zero or more characters.

2. Find Values That End With a Specific Character

You can use % before a character to search for values that end with that character.

SELECT *
FROM Employees
WHERE FirstName LIKE '%n';

This query returns employees whose first names end with the letter n.

The pattern '%n' matches any number of characters before the final n.

3. Find Values That Contain Specific Text

To find employees whose first names contain the letter sequence ar, use:

SELECT *
FROM Employees
WHERE FirstName LIKE '%ar%';

This query searches for ar anywhere in the FirstName column.

The % wildcard on both sides allows any characters before and after the specified text.

4. Search for a Word Within a String

You can also search for a specific word or phrase within a text column.

SELECT *
FROM Employees
WHERE Email LIKE '%example%';

This query returns records where the Email column contains the text example.

SQL Server LIKE with Underscore (_) Wildcard

The underscore (_) wildcard represents exactly one character.

Unlike %, which can represent multiple characters, _ matches only a single character.

1. Match a Single Character

The following query finds first names containing exactly four characters:

SELECT *
FROM Employees
WHERE FirstName LIKE '____';

Each underscore represents one character. Therefore, the pattern contains four underscores and matches names with exactly four characters.

2. Match a Character at a Specific Position

You can use underscores to search for characters at specific positions.

SELECT *
FROM Employees
WHERE FirstName LIKE 'J_hn';

This query matches a four-character name that starts with J, ends with hn, and contains any single character in the second position.

3. Combine Percentage and Underscore Wildcards

You can combine % and _ in the same pattern.

SELECT *
FROM Employees
WHERE FirstName LIKE 'A___%';

This pattern matches first names that start with A and contain at least four characters.

The three underscores require three additional characters after A, while % allows any remaining characters.

SQL Server LIKE with Character List ([ ])

The square brackets ([ ]) wildcard allow you to specify a list or range of characters. SQL Server matches a single character from the specified list.

1. Search Using a Character List

To find employees whose first names start with either A or J, use:

SELECT *
FROM Employees
WHERE FirstName LIKE '[AJ]%';

The query returns names beginning with A or J.

2. Search Using a Character Range

You can specify a range of characters inside square brackets.

SELECT *
FROM Employees
WHERE FirstName LIKE '[A-D]%';

This query finds first names beginning with a character in the range from A to D.

3. Search for Multiple Character Ranges

You can combine character ranges and individual characters.

SELECT *
FROM Employees
WHERE FirstName LIKE '[A-DJ-M]%';

This pattern matches names beginning with a character in either the A–D or J–M range.

SQL Server LIKE with Negated Character List ([^ ])

The [^ ] wildcard matches any single character that is not included in the specified list or range.

For example:

SELECT *
FROM Employees
WHERE FirstName LIKE '[^AJ]%';

This query returns employees whose first names do not start with A or J.

You can also use a negated character range:

SELECT *
FROM Employees
WHERE FirstName LIKE '[^A-D]%';

This pattern matches names that do not start with a character from A through D.

SQL Server NOT LIKE Operator

The NOT LIKE operator returns records that do not match a specified pattern.

For example, to find employees whose first names do not start with A, use:

SELECT *
FROM Employees
WHERE FirstName NOT LIKE 'A%';

This query excludes employees whose first names begin with A.

You can also use NOT LIKE to exclude email addresses containing a specific domain:

SELECT *
FROM Employees
WHERE Email NOT LIKE '%example.com';

The query returns records where the Email value does not end with example.com.

SQL Server LIKE with Multiple Conditions

You can combine multiple LIKE conditions using logical operators such as AND and OR.

1. LIKE with AND Operator

The AND operator requires all specified conditions to be true.

SELECT *
FROM Employees
WHERE FirstName LIKE 'A%'
AND Email LIKE '%example.com';

This query returns employees whose first names start with A and whose email addresses end with example.com.

2. LIKE with OR Operator

The OR operator returns records when at least one condition is true.

SELECT *
FROM Employees
WHERE FirstName LIKE 'A%'
OR FirstName LIKE 'J%';

This query returns employees whose first names start with either A or J.

3. Combine LIKE and NOT LIKE

You can also combine LIKE and NOT LIKE conditions.

SELECT *
FROM Employees
WHERE Email LIKE '%example.com'
AND FirstName NOT LIKE 'A%';

This query returns employees whose email addresses contain the specified domain but whose first names do not start with A.

SQL Server LIKE with the WHERE Clause

The LIKE operator is generally used with the WHERE clause to filter records based on text patterns.

For example, to find employees working in departments whose names start with F, use:

SELECT EmployeeID, FirstName, LastName, Department
FROM Employees
WHERE Department LIKE 'F%';

This query returns employees working in departments whose names begin with F.

You can use the LIKE operator with any compatible character column, including CHAR, VARCHAR, NCHAR, and NVARCHAR.

SQL Server LIKE with Escape Character

Sometimes, you may need to search for special wildcard characters such as % or _ as ordinary text instead of using them as wildcards.

SQL Server provides the ESCAPE keyword for this purpose.

For example, suppose a table contains product codes with a percentage symbol. You can search for the actual % character using an escape character.

SELECT *
FROM Products
WHERE ProductCode LIKE '%!%%' ESCAPE '!';

Here:

  • ! is the escape character.
  • !% represents a literal percentage symbol.
  • The final % represents any characters after the percentage symbol.

Similarly, you can search for an actual underscore:

SELECT *
FROM Products
WHERE ProductCode LIKE '%!_%' ESCAPE '!';

This query searches for product codes containing an actual underscore character.

SQL Server LIKE with ORDER BY

You can combine LIKE with the ORDER BY clause to filter matching records and sort the results.

SELECT EmployeeID, FirstName, LastName, Department
FROM Employees
WHERE FirstName LIKE 'A%'
ORDER BY FirstName ASC;

This query retrieves employees whose first names start with A and sorts the results alphabetically.

To sort in descending order, use:

SELECT EmployeeID, FirstName, LastName
FROM Employees
WHERE FirstName LIKE '%n'
ORDER BY FirstName DESC;

SQL Server LIKE Performance Considerations

The LIKE operator can be useful for searching large amounts of text, but the pattern you choose can affect query performance.

When a pattern begins with a fixed character or sequence, such as 'A%', SQL Server may be able to use an appropriate index to locate matching records efficiently.

However, a pattern that begins with a wildcard, such as '%A%', generally makes it harder for SQL Server to perform an index seek on a regular B-tree index.

Consider the following queries:

-- Starts with A
SELECT *
FROM Employees
WHERE FirstName LIKE 'A%';

-- Contains A anywhere
SELECT *
FROM Employees
WHERE FirstName LIKE '%A%';

The first query may benefit from an index on the FirstName column, depending on the query plan and database conditions. The second query usually requires more extensive scanning.

For better query performance:

  • Avoid leading wildcards when possible.
  • Retrieve only the columns you need instead of using SELECT *.
  • Create appropriate indexes for frequently searched columns.
  • Review execution plans for queries that run against large tables.
  • Use full-text search when your application requires advanced word-based searching across large text columns.

The best approach depends on the table size, indexing strategy, data distribution, and search requirements.