SQL Server LIKE operator with Examples- SQL Server Tutorial (2023)

Learning Objective

The objective of this SQL Server tutorial is to teach you how to use the LIKE logical operator to match a pattern.

What is LIKE operator in SQL Server?

The SQL Server LIKE operator performs flexible pattern matching with the help of wild cards which eliminates the need to specify the exact or entire pattern. It can be used in SELECT, UPDATE or DELETE query and is an alternative to IN, = and !=.

Operation

The SQL Server LIKE operator follows the WHERE clause in a SQL query and returns those rows in the resultset which match the pattern specified. It is also possible to negate the effect of the LIKE operator by adding NOT (which makes it NOT LIKE) so that the query returns resultset which does not match the pattern specified. The different wild cards that can be used with the LIKE operator are discussed below.

  1. % (Percentage) – Percentage is used to match one or more characters in a pattern and can be specified at the beginning, end or in the middle of the pattern to match.
  2. _ (Underscore) – Underscore is used to match a single character in a pattern and can be specified at the beginning, end or in the middle of the pattern to match.
  3. [] Bracket – Bracket is used to match a single character from a list or range of characters and can be specified at the beginning, end or in the middle of the pattern to match.
  4. [^] Caret – Caret within bracket is followed by a list of characters or a range of characters to match a single character in the pattern negatively i.e. to check and ensure that the pattern evaluated does not contain that character(s) in the specified position.
  5. (!) Exclamation – Exclamation is used as escape character to match characters that are usually used as wild cards (i.e. all the characters mentioned so far) in a LIKE query. The ESCAPE character tells the LIKE operator to treat any wild card character specified after the escape character as a normal regular character in a pattern.

SQL Server LIKE operator Syntax

The basic syntax of SQL Server LIKE operator is as follows.

SELECT expressions FROM tables WHERE expression LIKE | NOT LIKE [pattern [ESCAPE escape_character]]; 

In this syntax,

  • expressions – expressions defined here the column(s) or calculation you want to retrieve. If you want to retrieve all the columns simply use * in the place of expressions.
  • tables – one or more than one table from where you want to retrieve data.
  • WHERE conditions – Optional. This is used to specify some conditions while selecting data. In case you are not using WHERE clause all the rows available will be selected.
  • LIKE – Logical operator used to match a string pattern.
  • NOT LIKE– Logical operator used to negatively match a string pattern.
  • pattern – String consisting of either alphabet, number, special character (including wild card characters) or one or all of these.
  • ESCAPE – Optional. Keyword used to specify the character used as escape character.
  • escape character– Optional. The character used as escape character. Usually !.

SQL Server LIKE operator Examples

NOTE: Columns are also referred to as fields or attributes and the terms are used interchangeably.

Let us see how it works with all the wild cards and query types. Suppose we have a customer table with the below details in it. We will query it using LIKE to demonstrate the different usage scenarios.

customer_idfirst_namelast_namecustomer_emailcustomer_comment
1AliciaKeysalicia_alicia@gmail.comI want a 15% discount on the next purchase
2IndiRossiindi4u@gmail.comWhen is the next big sale?
3JackSmithjs2-k@hotmail.comIs there any sale next week? What % discount?
4PeteWilliamsmyname__Wpete@yahoo.co.in@help, customer service
5CaseyKugelmanckugel1999@yahoo.co.in@help, please call me back
6LaurenCrowlcc2k02@gmail.comWhere are the new product updates?
7StephenFlemingfire_stephen_01@gmail.comno comments..
8VanessaMayvanessamayU@hotmail.comNeed a call back and 15% refund

1)SQL Server LIKE – % (percentage) wild card examples

Let us see how we can use the % symbol at different positions in a pattern for matching multiple characters using the customers table.

a) Percent at end example

The below query is an example of using % to list customer comments from the customers table which begin with the letter ‘w’.

(Video) SQL LIKE Operator | SQL Tutorial for Beginners | 2021

SELECT * FROM customers WHERE customer_comment LIKE 'w%';

The query will generate the following output.

customer_idfirst_namelast_namecustomer_emailcustomer_comment
2IndiRossiindi4u@gmail.comWhen is the next big sale?
6LaurenCrowlcc2k02@gmail.comWhere are the new product updates?

b) Percent at beginning example

The below query is an example of using % to list customers who have their email id’s in gmail i.e. email id’s which end in ‘gmail.com’.

SELECT * FROM customers WHERE customer_email LIKE '%@gmail.com';

The query will generate the following output.

customer_idfirst_namelast_namecustomer_emailcustomer_comment
1AliciaKeysalicia_alicia@gmail.comI want a 15% discount on the next purchase
2IndiRossiindi4u@gmail.comWhen is the next big sale?
6LaurenCrowlcc2k02@gmail.comWhere are the new product updates?
7StephenFlemingfire_stephen_01@gmail.comno comments..

c) Percent in the middle example

The below query is an example of using % to list customer email id’s which have ‘co.’ in them.

SELECT * FROM customers WHERE customer_email LIKE '%co.%';

The query will generate the following output.

customer_idfirst_namelast_namecustomer_emailcustomer_comment
4PeteWilliamsmyname__Wpete@yahoo.co.in@help, customer service
5CaseyKugelmanckugel1999@yahoo.co.in@help, please call me back

2)SQL Server LIKE – _ (underscore) wild card examples

Let us see how we can use the _ symbol at different positions in a pattern to match a single character using the customers table.

The below query is an example which uses _ to list customer names which have ‘a’ as the second character.

SELECT * FROM customers WHERE first_name LIKE '_a%';

The above query will generate the following output.

customer_idfirst_namelast_namecustomer_emailcustomer_comment
3JackSmithjs2-k@hotmail.comIs there any sale next week? What % discount?
5CaseyKugelmanckugel1999@yahoo.co.in@help, please call me back
6LaurenCrowlcc2k02@gmail.comWhere are the new product updates?
8VanessaMayvanessamayU@hotmail.comNeed a call back and 15% refund

3)SQL Server LIKE – [] (bracket) wild card examples

Let us see how we can use the [] symbol at different positions in a pattern to match a character from a list or range using the customers table.

(Video) Sql Server - Like Operator | חדש 2021 ❤️‍🔥

a) Character list example

The below query is an example which uses [] to list customer names which begin with either an ‘a’, ‘c’ or ‘l’ as specified in the character list within bracket.

SELECT * FROM customers WHERE first_name LIKE '[acl]%'; 

The above query will generate the following output.

customer_idfirst_namelast_namecustomer_emailcustomer_comment
1AliciaKeysalicia_alicia@gmail.comI want a 15% discount on the next purchase
5CaseyKugelmanckugel1999@yahoo.co.in@help, please call me back
6LaurenCrowlcc2k02@gmail.comWhere are the new product updates?

b) Character range example

The below query is an example which uses [] to list customer names which begin with any character between the specified range of a to l (which includes 12 characters from the alphabet).

SELECT * FROM customers WHERE first_name LIKE '[a-l]%'; 

The above query will generate the following output.

customer_idfirst_namelast_namecustomer_emailcustomer_comment
1AliciaKeysalicia_alicia@gmail.comI want a 15% dicounnt on the next purchase
2IndiRossiindi4u@gmail.comWhen is the next big sale?
3JackSmithjs2-k@hotmail.comIs there any sale next week? What % discount?
5CaseyKugelmanckugel1999@yahoo.co.in@help, please call me back
6LaurenCrowlcc2k02@gmail.comWhere are the new product updates?

4)SQL Server LIKE – [^] caret within bracket examples

Let us see how we can use the [^] symbol at different positions in a pattern to negatively match a character from a list or range using the customers table.

a) Character list example

The below query is an example which uses [^] to list customer names which do not begin with either an ‘a’, ‘c’ or ‘l’ as specified in the character list within bracket.

SELECT * FROM customers WHERE first_name LIKE '[^acl]%';

The above query will generate the following output.

customer_idfirst_namelast_namecustomer_emailcustomer_comment
2IndiRossiindi4u@gmail.comWhen is the next big sale?
3JackSmithjs2-k@hotmail.comIs there any sale next week? What % discount?
4PeteWilliamsmyname__Wpete@yahoo.co.in@help, customer service
7StephenFlemingfire_stephen_01@gmail.comno comments..
8VanessaMayvanessamayU@hotmail.comNeed a call back and 15% refund

b) Character range example

The below query is an example which uses [^] to list customer names which do not begin with any character between the specified range of a to l (which includes 12 characters from the alphabet).

SELECT * FROM customers WHERE first_name LIKE '[^a-l]%';

The above query will generate the following output.

(Video) What is LIKE Logical Operator in SQL Server - SQL Server /TSQL Tutorial Part 123

customer_idfirst_namelast_namecustomer_emailcustomer_comment
4PeteWilliamsmyname__Wpete@yahoo.co.in@help, customer service
7StephenFlemingfire_stephen_01@gmail.comno comments..
8VanessaMayvanessamayU@hotmail.comNeed a call back and 10% refund

5)SQL Server LIKE –! Escape character example

Let us see how we can use the ! escape character to treat wild card characters as normal regular characters in a pattern search so that the resultset return rows with them in it.

The below query is an example where the percentage symbols at the beginning and end are wild cards for random pattern matching but the percentage after ! is to be treated as a normal character in the pattern search and returned in the resultset. The aim is to retrieve percentage values from the table. Likewise it can be used for _, ^, [ and ].

SELECT * FROM customers WHERE customer_comment LIKE ('%15!%%') ESCAPE '!';

The above query will generate the following output.

customer_idfirst_namelast_namecustomer_emailcustomer_comment
1AliciaKeysalicia_alicia@gmail.comI want a 15% discount on the next purchase
8VanessaMayvanessamayU@hotmail.comNeed a call back and 15% refund

6)SQL Server LIKE – NOT LIKE example

Let us see how we can use NOT LIKE for negative pattern matching so that the resultset does not contain the pattern matched.

The below query is an example which uses the NOT LIKE operator to return rows from the customers table where the customer email id is not a gmail id.

SELECT * FROM customers WHERE customer_email NOT LIKE '%gmail.com';

The above query will generate the following output.

customer_idfirst_namelast_namecustomer_emailcustomer_comment
3JackSmithjs2-k@hotmail.comIs there any sale next week? What % discount?
4PeteWilliamsmyname__Wpete@yahoo.co.in@help, customer service
5CaseyKugelmanckugel1999@yahoo.co.in@help, please call me back
8VanessaMayvanessamayU@hotmail.comNeed a call back and 15% refund

7) SQL Server LIKE – UPDATE query example

Let us see one example of how we can use LIKE in an update query to modify records that match (or does not match if we use NOT LIKE) the pattern specified with LIKE.

The below query is an example which removes the customer comments for customers having yahoo email ids.

UPDATE customers SET customer_comment=NULL WHERE customer_email like '%yahoo%';

We can check the update by running below SELECT query that will display the update.

(Video) SQL LIKE OPERATOR - A BEGINNER SQL SERVER TUTORIAL

SELECT * FROM customers WHERE customer_email like '%yahoo%';
customer_idfirst_namelast_namecustomer_emailcustomer_comment
4PeteWilliamsmyname__Wpete@yahoo.co.inNULL
5CaseyKugelmanckugel1999@yahoo.co.inNULL

8) SQL Server LIKE – DELETE query example

Let us see one example of how we can use LIKE in a DELETE query to delete records that match (or does not match if we use NOT LIKE) the pattern specified with LIKE.

The below query is an example which deletes records of customers from the customers table who have yahoo email ids.

DELETE FROM customers WHERE customer_email like '%yahoo%';

We can check the update by running below SELECT query which shows only 6 records after the deletion of 2.

SELECT * FROM customers;
customer_idfirst_namelast_namecustomer_emailcustomer_comment
1AliciaKeysalicia_alicia@gmail.comI want a 15% discount on the next purchase
2IndiRossiindi4u@gmail.comWhen is the next big sale?
3JackSmithjs2-k@hotmail.comIs there any sale next week? What % discount?
6LaurenCrowlcc2k02@gmail.comWhere are the new product updates?
7StephenFlemingfire_stephen_01@gmail.comno comments..
8VanessaMayvanessamayU@hotmail.comNeed a call back and 15% refund

Was this tutorial helpful?

YesNo

« Previous: SQL Server BETWEEN

SQL SERVER ALIAS :Next »

(Video) SQL Tutorial - 23: The LIKE Operator and Wildcard Characters

FAQs

What does like %% mean in SQL? ›

The % is a wildcard that matches any character, and any number of characters. So a LIKE % matches everything. In your case, the LIKE '% %' matches anything that has a space in it.

How to pass multiple values in like operator in SQL? ›

There are two wildcards used in conjunction with the LIKE operator. The percent sign represents zero, one or multiple characters. The underscore represents a single number or character.

How do you use the like and in operators in an SQL query? ›

LIKE Operator in SQL

In a WHERE clause, the LIKE operator is used to look for a certain pattern in a column. In SQL, it has two wildcard characters, such as: Percentage symbol (%): It is a substitution for zero, one, or more characters. Underscore symbol (_): It is a substitution for a single character.

How do you use like and in operator? ›

you cannot. where name like 'sasho' or name like 'shashi%' or name like 'rags'; the LIKE operation is not permitted to be used with IN.

How do you use the LIKE operator in an SQL query quizlet? ›

How do you use the LIKE operators in an SQL query? The LIKE operator is used to get all the rows in a table whose column values match a specified pattern in a column. For this reason, we use a wildcard character '%' and _(Underscore).

How do you do like in SQL? ›

Using SQL LIKE with the [ ] wildcard characters

For example, let's say to list all the rows where first names third character start with I or K. Instead of writing multiple LIKE conditions, we can place the pattern matching set in the third position and close it in the square.

Why use like instead of in SQL? ›

LIKE is what you would use in a search query. It also allows wildcards like _ (simple character wildcard) and % (multi-character wildcard). = should be used if you want exact matches and it will be faster. Save this answer.

Can I use like in select SQL? ›

SQL Like operator can be used with any query with where clause. So we can use it with Select, Delete, Update etc.

What is %s means in SQL? ›

%s is a placeholder used in functions like sprintf. Check the manual for other possible placeholders. $sql = sprintf($sql, "Test"); This would replace %s with the string "Test". It's also used to make sure that the parameter passed actually fits the placeholder.

What is like 0 9 in SQL? ›

[0-9] is not some type of regular expression defined to just match digits. Any range in a LIKE pattern matches characters between the start and end character according to collation sort order. So you get these results because under your default collation these characters sort after 0 but before 9 .

What is like and notlike in SQL? ›

The SQL LIKE and NOT LIKE operators are used to find matches between a string and a given pattern. They are part of standard SQL and work across all database types, making it essential knowledge for all SQL users.

Videos

1. Sql Server Where Clause Like operator | SQL Server tutorial
(YasirFazal)
2. SQL Tutorial for Data Analysis 12: LIKE Operator
(Ram Kedem)
3. SQL Tutorial #16 - SQL LIKE Operator | How to use LIKE in SQL
(Software Testing Mentor)
4. SQL SERVER DBA tutorials || Demo - 3 || by Mr. Suresh Vadde On 10-02-2023 @8AM IST
(Durga Software Solutions)
5. Like and Between Operators in SQL | Like Operator in SQL Server | Between Operator in SQL Server
(Intellipaat)
6. LIKE Operator In SQL Server With Examples
(Ajmath Online)
Top Articles
Latest Posts
Article information

Author: Foster Heidenreich CPA

Last Updated: 03/13/2023

Views: 6004

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Foster Heidenreich CPA

Birthday: 1995-01-14

Address: 55021 Usha Garden, North Larisa, DE 19209

Phone: +6812240846623

Job: Corporate Healthcare Strategist

Hobby: Singing, Listening to music, Rafting, LARPing, Gardening, Quilting, Rappelling

Introduction: My name is Foster Heidenreich CPA, I am a delightful, quaint, glorious, quaint, faithful, enchanting, fine person who loves writing and wants to share my knowledge and understanding with you.