SQL Wildcard Characters (original) (raw)
SQL **wildcard characters are powerful tools that enable advanced **pattern matching in string data. They are especially useful when working with the **LIKE and **NOT LIKE operators, allowing for efficient searches based on partial matches or specific patterns. By using SQL wildcard characters, we can greatly enhance the functionality of our SQL queries and perform sophisticated data retrieval tasks.
In this comprehensive article, we will explain the various **SQL wildcard characters, how they work, and provide practical examples to help us use them effectively in our queries.
What Are SQL Wildcard Characters?
**SQL wildcard Charactersare special characters used in SQL queries to search for **patterns within string data. These wildcards can help us perform partial searches, matching parts of a string or allowing **complex filtering based on patterns. They are most commonly used with the LIKE and **NOT LIKE clauses in SQL. Using **wildcard characters in SQL allows us to search for patterns rather than **exact matches, which is especially useful in cases where we only know part of the string we are looking for
**Syntax
SELECT column1,column2 FROM table_name
WHERE column LIKE wildcard_operator;
**Key Terms:
- **column1,column2: fields in the table
- **table_name: name of the table
- **column: name of the field used for filtering data
**Types of SQL Wildcard Characters
There are several wildcard characters in SQL, each serving a different purpose in pattern matching. Let’s break down the most common wildcard characters and their usage:
**Wildcard Character | **Description |
---|---|
****%** | Represents zero or more characters. |
**_ | Represents exactly one character. |
**[ ] | Represents a range of characters, used to match any single character within the range. |
**[ ] with **^ or ****!** | Matches any character that is **NOT in the specified range. |
**Example of SQL Wildcard Characters
Let’s dive into **practical examples of how to use these wildcard characters in SQL queries. The **Customer table contains data about **customers, including columns such as **CustomerID, **CustomerName, **LastName, **Country, **Age, and **Phone. We will use a **Customer table to demonstrate various search patterns
Customer Table
1. Using the % Wildcard
The ****%** wildcard is used to substitute for **zero or more characters. It’s very flexible and is commonly used for **matching partial strings.
**Example 1: Records Starting with a Specific Letter
To fetch records where the **CustomerName starts with the letter ****'A'**. This query will return all records where the CustomerName
starts with the letter **A, regardless of what comes after it.
**Query:
SELECT *
FROM Customer
WHERE CustomerName LIKE 'A%';
**Output
Table-1
Example 2: Records Ending with a Specific Letter
To fetch records from the **Customer table with NAME ending with the letter 'A'. This query retrieves all customers whose name ends with **A, regardless of what comes before it.
**Query:
SELECT *
FROM Customer
WHERE CustomerName LIKE '%A';
**Output
Table - 2
Example 3: Records Containing a Specific Letter at Any Position
To fetch records from the **Customer table with NAME with the letter 'A' at any position. This query will return records where **A appears anywhere within the name, whether at the **beginning, **middle, or **end.
**Query
SELECT * FROM Customer WHERE CustomerName LIKE '%A%';
**Output
Table -3
Example 4: Fetching Records with 'ra' in Country Field and Removing Duplicates
To fetch records from the **Customer table where the **Country contains the substring ****'ra'** at any position, and ensure the result set does not contain duplicate data:
**Query:
SELECT DISTINCT * FROM Customer WHERE Country LIKE '%ra%';
**Output
Table - 4
2. Using the _ Wildcard
The **_ wildcard is used to substitute for exactly **one character. This is useful when we know part of the string but need to match one specific unknown character.
**Example 1: Records with a Specific Prefix and Any Three Letters
To fetch records where the **CustomerName starts with ****'Nav'** and is followed by any three characters. This query will retrieve all records where the name starts with **Nav and has exactly three more characters after it (e.g., **Naveen).
**Query:
SELECT * FROM Customer WHERE CustomerName LIKE 'Nav___';
**Output
Table - 5
Example 2: Records with a Specific Length
To fetch records from the Customer table with Country containing a total of **7 characters. The query will return all records where the Country
field has exactly seven characters.
**Query:
SELECT * FROM Customer WHERE Country LIKE '_______';
**Output
Table - 6
3. Using the [ ] Wildcard
The **[ ] wildcard allows us to specify a set or range of characters that we want to match. It is especially useful when we need to search for specific characters within a defined **range or **group.
Example 1: Matching One Character from a Set
To fetch records from the Customer table with LastName containing letters 'a, 'b', or 'c'. This query will return records where the last name starts with **A, **B, or **C.
**Query
SELECT * FROM Customer WHERE LastName REGEXP '[A-C]';
**Output
Table - 7
Example 2: Matching Characters Outside a Range
To fetch records from the **Customer table with LastName not containing letters 'y', or 'z'. This query retrieves customers whose last names do not contain the letters **Y or **Z.
**Query
SELECT * FROM Students WHERE LastName NOT LIKE '%[y-z]%';
**Output
Table - 8
4. Combining Both % and _ Wildcard
We can also combine ****%** and **_ wildcards to perform more complex searches. The ****%** wildcard represents **any sequence of characters, while the **_ wildcard represents exactly one character. By combining both, we can create intricate patterns that match a wide range of string data with greater precision, allowing us to **filter data based on **multiple criteria.
**Example: Matching Specific Digits in a Phone Number
To fetch records where the **Phone number starts with ****'8'** in the first position, has any two characters in the second and third positions, and then has ****'5'** in the fourth position.
**Query
SELECT * FROM Student WHERE PHONE LIKE '8__5%';
**Output
Table - 9
Conclusion
SQL **wildcard characters are an important for performing **advanced text matching and **filtering within our database queries. By using ****%**, **_, and other wildcards, we can easily query datasets with **partial or **complex patterns. Understanding how to use these characters effectively will significantly enhance our ability to extract meaningful data from our tables. Mastering wildcards allows us to build more **dynamic and **flexible queries, which can be especially useful for **data analysis and **reporting tasks.