Languages
[Edit]
EN

MS SQL Server - LIKE operator

0 points
Created by:
Barmar
338

In this article, we would like to show you how to use LIKE operator in MS SQL Server.

Quick solution:

 

We use LIKE operator in a WHERE clause to find rows that contan column cell that matches indicated pattern.

There are two wildcards used with the LIKE operator:

  • % - matches 0 or more characters (0, 1, 2, 3, ...),
  • _ - matches any single character (requres in _ place some character).
LIKE operatorDescription
Finds any rows that username cell starts with prefix text.
Finds any rows that username cell ends with postfix text.
Finds any rows that username cell has middle text somewhere inside.

Finds any rows that username cell:

  • starts with part1 text
  • and has part2 text somewhere inside
  • and ends with part3 text.

Practical example

Edit

Let's say we have users database that contains usernames.

Listed patterns are matched with presented example texts:

To show how the LIKE operator works, we will use the following table:

MS SQL Server - example data used with LIKE operator
MS SQL Server - example data used with LIKE operator

Note:

At the end of this article you can find database preparation SQL queries.

Example

Edit

In this example, we will display Jonh user row using some LIKE operator cases taken from above patterns. 

Queries:

Result:

MS SQL Server - LIKE operator result
MS SQL Server - LIKE operator result

Database preparation

Edit

create_tables.sql file:

insert_data.sql file:

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

MS SQL Server

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join