SQL Programming
  • Introduction to SQL
  • Data Definition Language
    • DATA TYPES
    • CREATE DATABASE
    • DROP DATABASE
    • CREATE TABLE Statement
    • CONSTRAINTS
      • SQL PRIMARY KEY Constraint
      • SQL NOT NULL Constraint
      • SQL UNIQUE Constraint
      • SQL FOREIGN KEY Constraint
      • SQL DEFAULT Constraint
      • SQL CHECK Constraint
    • DROP & TRUNCATE TABLE Statement
    • ALTER TABLE Statement
    • AUTO INCREMENT Field
    • SQL SELECT INTO Statement
    • CREATE INDEX
    • DDL Practice Problems
  • Data Manipulation Language
    • INSERT INTO Statement
    • INSERT INTO SELECT Statement
    • DELETE Statement
    • UPDATE Statement
    • DML Practice Problems
  • SQL NULL Values
  • SQL Comments
  • Data Query Language
    • SQL SELECT DISTINCT Statement
    • SQL WHERE Clause
      • SQL And, Or, Not (Logical Operators)
      • SQL Wildcards
      • SQL LIKE Operator
      • SQL IN Operator
      • SQL BETWEEN Operator
      • SQL WHERE Exercises
    • SQL TOP CLAUSE
    • SQL Order By
    • SQL Server Scalar Functions (Non-aggregate)
      • SQL MIN() and MAX()
      • SQL COUNT(), AVG() and SUM() Functions
      • Functions Problems
    • SQL ISNULL Functions
    • SQL Aliases
    • SQL CASE Statement
    • SQL SET Operator
    • SQL GROUP BY Statement
    • SQL HAVING Clause
    • Analytical Function
    • SQL Joins
      • SQL INNER JOIN Keyword
      • SQL LEFT JOIN Keyword
      • SQL RIGHT JOIN Keyword
      • SQL FULL OUTER JOIN Keyword
      • SQL Self JOIN
      • SQL CROSS JOIN
    • SQL Subqueries
    • SQL EXISTS Operator
    • SQL ANY and ALL Operators
  • CREATE VIEWS
  • Common Table Expressions (CTE)
  • Data Control Language
  • SQL Stored Procedures for SQL Server
  • Practice Papers
  • SQL PROJECT
Powered by GitBook
On this page
  • SQL Wildcard Characters
  • Demo Database
  • Using the % Wildcard
  • Using the _ Wildcard
  • Using the [charlist] Wildcard
  • Using the [!charlist] Wildcard

Was this helpful?

  1. Data Query Language
  2. SQL WHERE Clause

SQL Wildcards

PreviousSQL And, Or, Not (Logical Operators)NextSQL LIKE Operator

Last updated 6 years ago

Was this helpful?

SQL Wildcard Characters

A wildcard character is used to substitute any other character(s) in a string.

Wildcard characters are used with the SQL LIKE operator. The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

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 character

Here are some examples showing different LIKE operators with '%' and '_' wildcards:

Demo Database

Below is a selection from the "Customers" table :

Using the % Wildcard

The following SQL statement selects all customers with a City starting with "ber":

Example

SELECT * FROM Customers
WHERE City LIKE 'ber%';

The following SQL statement selects all customers with a City containing the pattern "es":

Example

SELECT * FROM Customers
WHERE City LIKE '%es%';

Using the _ Wildcard

The following SQL statement selects all customers with a City starting with any character, followed by "erlin":

Example

SELECT * FROM Customers
WHERE City LIKE '_erlin';

The following SQL statement selects all customers with a City starting with "L", followed by any character, followed by "n", followed by any character, followed by "on":

Example

SELECT * FROM Customers
WHERE City LIKE 'L_n_on';

Using the [charlist] Wildcard

The following SQL statement selects all customers with a City starting with "b", "s", or "p":

Example

SELECT * FROM Customers
WHERE City LIKE '[bsp]%';

The following SQL statement selects all customers with a City starting with "a", "b", or "c":

Example

SELECT * FROM Customers
WHERE City LIKE '[a-c]%';

Using the [!charlist] Wildcard

The two following SQL statements select all customers with a City NOT starting with "b", "s", or "p":

Example

SELECT * FROM Customers
WHERE City LIKE '[!bsp]%';

Or:

Example

SELECT * FROM Customers
WHERE City NOT LIKE '[bsp]%';