What are the wildcard characters used with like operator in an SQL query write the use of any one with two support examples?


We have already discussed about the SQL LIKE operator, which is used to compare a value to similar values using the wildcard operators.

SQL supports two wildcard operators in conjunction with the LIKE operator which are explained in detail in the following table.

Sr.No. Wildcard & Description
1

The percent sign (%)

Matches one or more characters.

Note − MS Access uses the asterisk (*) wildcard character instead of the percent sign (%) wildcard character.

2

The underscore (_)

Matches one character.

Note − MS Access uses a question mark (?) instead of the underscore (_) to match any one character.

The percent sign represents zero, one or multiple characters. The underscore represents a single number or a character. These symbols can be used in combinations.

Syntax

The basic syntax of a '%' and a '_' operator is as follows.

SELECT * FROM table_name WHERE column LIKE 'XXXX%' or SELECT * FROM table_name WHERE column LIKE '%XXXX%' or SELECT * FROM table_name WHERE column LIKE 'XXXX_' or SELECT * FROM table_name WHERE column LIKE '_XXXX' or SELECT * FROM table_name WHERE column LIKE '_XXXX_'

You can combine N number of conditions using the AND or the OR operators. Here, XXXX could be any numeric or string value.

Example

The following table has a number of examples showing the WHERE part having different LIKE clauses with '%' and '_' operators.

Sr.No. Statement & Description
1

WHERE SALARY LIKE '200%'

Finds any values that start with 200.

2

WHERE SALARY LIKE '%200%'

Finds any values that have 200 in any position.

3

WHERE SALARY LIKE '_00%'

Finds any values that have 00 in the second and third positions.

4

WHERE SALARY LIKE '2_%_%'

Finds any values that start with 2 and are at least 3 characters in length.

5

WHERE SALARY LIKE '%2'

Finds any values that end with 2.

6

WHERE SALARY LIKE '_2%3'

Finds any values that have a 2 in the second position and end with a 3.

7

WHERE SALARY LIKE '2___3'

Finds any values in a five-digit number that start with 2 and end with 3.

Let us take a real example, consider the CUSTOMERS table having the following records.

+----+----------+-----+-----------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 25 | Delhi | 1500.00 | | 3 | kaushik | 23 | Kota | 2000.00 | | 4 | Chaitali | 25 | Mumbai | 6500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 6 | Komal | 22 | MP | 4500.00 | | 7 | Muffy | 24 | Indore | 10000.00 | +----+----------+-----+-----------+----------+

The following code block is an example, which would display all the records from the CUSTOMERS table where the SALARY starts with 200.

SQL> SELECT * FROM CUSTOMERS WHERE SALARY LIKE '200%';

This would produce the following result.

+----+----------+-----+-----------+----------+ | ID | NAME | AGE | ADDRESS | SALARY | +----+----------+-----+-----------+----------+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 3 | kaushik | 23 | Kota | 2000.00 | +----+----------+-----+-----------+----------+

Summary: in this tutorial, you will learn how to use the SQL Server LIKE to check whether a character string matches a specified pattern.

SQL Server LIKE operator overview

The SQL Server LIKE is a logical operator that determines if a character string matches a specified pattern. A pattern may include regular characters and wildcard characters. The LIKE operator is used in the WHERE clause of the SELECT, UPDATE, and DELETE statements to filter rows based on pattern matching.

The following illustrates the syntax of the SQL Server LIKE operator:

column | expression LIKE pattern [ESCAPE escape_character]

Code language: SQL (Structured Query Language) (sql)

Pattern

The pattern is a sequence of characters to search for in the column or expression. It can include the following valid wildcard characters:

  • The percent wildcard (%): any string of zero or more characters.
  • The underscore (_) wildcard: any single character.
  • The [list of characters] wildcard: any single character within the specified set.
  • The [character-character]: any single character within the specified range.
  • The [^]: any single character not within a list or a range.

The wildcard characters makes the LIKE operator more flexible than the equal (=) and not equal (!=) string comparison operators.

Escape character

The escape character instructs the LIKE operator to treat the wildcard characters as the regular characters. The escape character has no default value and must be evaluated to only one character.

The LIKE operator returns TRUE if the column or expression matches the specified pattern.

To negate the result of the LIKE operator, you use the NOT operator as follows:

column | expression NOT LIKE pattern [ESCAPE escape_character]

Code language: SQL (Structured Query Language) (sql)

SQL Server LIKE examples

See the following customers table from the sample database:

What are the wildcard characters used with like operator in an SQL query write the use of any one with two support examples?

The % (percent) wildcard examples

The following example finds the customers whose last name starts with the letter z:

SELECT customer_id, first_name, last_name FROM sales.customers WHERE last_name LIKE 'z%' ORDER BY first_name;

Code language: SQL (Structured Query Language) (sql)
What are the wildcard characters used with like operator in an SQL query write the use of any one with two support examples?

The following example returns the customers whose last name ends with the string er:

SELECT customer_id, first_name, last_name FROM sales.customers WHERE last_name LIKE '%er' ORDER BY first_name;

Code language: SQL (Structured Query Language) (sql)
What are the wildcard characters used with like operator in an SQL query write the use of any one with two support examples?

The following statement retrieves the customers whose last name starts with the letter t and ends with the letter s:

SELECT customer_id, first_name, last_name FROM sales.customers WHERE last_name LIKE 't%s' ORDER BY first_name;

Code language: SQL (Structured Query Language) (sql)

What are the wildcard characters used with like operator in an SQL query write the use of any one with two support examples?

The _ (underscore) wildcard example

The underscore represents a single character. For example, the following statement returns the customers where the second character is the letter u:

SELECT customer_id, first_name, last_name FROM sales.customers WHERE last_name LIKE '_u%' ORDER BY first_name;

Code language: SQL (Structured Query Language) (sql)
What are the wildcard characters used with like operator in an SQL query write the use of any one with two support examples?

The pattern _u%

  • The first underscore character ( _) matches any single character.
  • The second letter u matches the letter u exactly
  • The third character % matches any sequence of characters

The [list of characters] wildcard example

The square brackets with a list of characters e.g., [ABC] represents a single character that must be one of the characters specified in the list.

For example, the following query returns the customers where the first character in the last name is Y or Z:

SELECT customer_id, first_name, last_name FROM sales.customers WHERE last_name LIKE '[YZ]%' ORDER BY last_name;

Code language: SQL (Structured Query Language) (sql)
What are the wildcard characters used with like operator in an SQL query write the use of any one with two support examples?

The [character-character] wildcard example

The square brackets with a character range e.g., [A-C] represent a single character that must be within a specified range.

For example, the following query finds the customers where the first character in the last name is the letter in the range A through C:

SELECT customer_id, first_name, last_name FROM sales.customers WHERE last_name LIKE '[A-C]%' ORDER BY first_name;

Code language: SQL (Structured Query Language) (sql)
What are the wildcard characters used with like operator in an SQL query write the use of any one with two support examples?

The [^Character List or Range] wildcard example

The square brackets with a caret sign (^) followed by a range e.g., [^A-C] or character list e.g., [ABC] represent a single character that is not in the specified range or character list.

For example, the following query returns the customers where the first character in the last name is not the letter in the range A through X:

SELECT customer_id, first_name, last_name FROM sales.customers WHERE last_name LIKE '[^A-X]%' ORDER BY last_name;

Code language: SQL (Structured Query Language) (sql)
What are the wildcard characters used with like operator in an SQL query write the use of any one with two support examples?

The NOT LIKE operator example

The following example uses the NOT LIKE operator to find customers where the first character in the first name is not the letter A:

SELECT customer_id, first_name, last_name FROM sales.customers WHERE first_name NOT LIKE 'A%' ORDER BY first_name;

Code language: SQL (Structured Query Language) (sql)
What are the wildcard characters used with like operator in an SQL query write the use of any one with two support examples?

SQL Server LIKE with ESCAPE example

First, create a new table for the demonstration:

CREATE TABLE sales.feedbacks ( feedback_id INT IDENTITY(1, 1) PRIMARY KEY, comment VARCHAR(255) NOT NULL );

Code language: SQL (Structured Query Language) (sql)

Second, insert some rows into the sales.feedbacks table:

INSERT INTO sales.feedbacks(comment) VALUES('Can you give me 30% discount?'), ('May I get me 30USD off?'), ('Is this having 20% discount today?');

Code language: SQL (Structured Query Language) (sql)

Third, query data from the sales.feedbacks table:

SELECT * FROM sales.feedbacks;

Code language: SQL (Structured Query Language) (sql)
What are the wildcard characters used with like operator in an SQL query write the use of any one with two support examples?

If you want to search for 30% in the comment column, you may come up with a query like this:

SELECT feedback_id, comment FROM sales.feedbacks WHERE comment LIKE '%30%';

Code language: SQL (Structured Query Language) (sql)
What are the wildcard characters used with like operator in an SQL query write the use of any one with two support examples?

The query returns the comments that contain 30% and 30USD, which is not what we expected.

To solve this issue, you need to use the ESCAPE clause:

SELECT feedback_id, comment FROM sales.feedbacks WHERE comment LIKE '%30!%%' ESCAPE '!';

Code language: SQL (Structured Query Language) (sql)
What are the wildcard characters used with like operator in an SQL query write the use of any one with two support examples?

In this query, the  ESCAPE clause specified that the character ! is the escape character. It instructs the LIKE operator to treat the % character as a literal string instead of a wildcard. Note that without the ESCAPE clause, the query would return an empty result set.

In this tutorial, you have learned how to use the SQL Server LIKE operator to check if a character string matches a specified pattern.