SQL Query to Match Any Part of String
Last Updated :
19 Sep, 2023
It is used for searching a string or a sub-string to find a certain character or group of characters from a string. We can use the LIKE Operator of SQL to search sub-strings. The LIKE operator is used with the WHERE Clause to search a pattern in a string of columns. The LIKE operator is used in conjunction with the two wildcard characters.
- Percentage sign( % ): It represents zero, one, or multiple characters of variable length.
- Underscore ( _ ): It represents one, single character of fixed length.
The Syntax of a LIKE Clause is
SELECT Column1, Column2 FROM TableName WHERE Column LIKE [Expression];
Example:
In this example, we will create a schema for our database and name it Emp_details. After that, we will create a table inside it with the name Emp_data and try to search for a sub-string from the table’s data.
Step 1: Create a database
In order to create a database we need to use the CREATE operator.
CREATE DATABASE Emp_details;
USE Emp_details;
Step 2: Create a table inside the database
In this step, we will create the table Emp_data inside the Emp_details database.
CREATE TABLE Emp_data(id INT,
first_name VARCHAR(255),
last_name VARCHAR(255),
Salary VARCHAR(255),
Age INT,
PRIMARY KEY(id));
Step 3: Insert data into the table
In order to insert the data inside the database we need to use INSERT operator.
INSERT INTO Emp_data (id, first_name, last_name, Salary,Age)
VALUES (1, "Yash", "Kumar", 30000,25),
(2, "Rahul", "Yadav", 40000,22),
(3, "Mohit", "Kumar", 50000,21),
(4, "Ritik", "Kumar", 30000,23),
(5, "Shubham", "Pal", 20000,25);
Output:

Emp_data Table
Using LIKE Clause with % to match any number of Characters
Example 1: To fetch records from the Emp_data Table with first_name starting with the letter ‘Y%’.
Query
SELECT * FROM Emp_data WHERE first_name LIKE 'Y%';
Output

output
Example 2: To fetch records from the Emp_data Table with first_name ending with the letter ‘%m’.
Query
SELECT * FROM Emp_data WHERE first_name LIKE '%m';
Output

output
Example 3: To fetch records from the Emp_data with first_name with the letter ‘h’ at any position.
Query
SELECT * FROM Emp_data WHERE first_name LIKE '%h%';
Output

output
Example4:
To fetch the records from Emp_data in which salary contains a number 50 in between.
Query
SELECT * FROM Emp_data WHERE salary LIKE '%50%';
Output

output
Using LIKE Clause with _ to match only one Character
Example 1: To fetch records from the Emp_data Table with first_name ending any letter but starting from ‘R’.
Query
SELECT * FROM Emp_data WHERE first_name LIKE 'R____';
Output

output
Example 2: To fetch a records from Emp_data table in which salary is starting with ‘3’ succeeding any two digits and finally ends with ’00’.
Query
SELECT * FROM Emp_data WHERE Salary LIKE '3__00';
Output

output
Example 3: To fetch records from the Emp_data Table with last_name starting with ‘K’.
Query
SELECT * FROM Emp_data WHERE last_name LIKE 'K____';
Output

output
Conclusion
To match any part of the string in SQL, we can use the LIKE operator with a wildcard. Typically, there are two types of wildcard operators utilized in SQL.
- %(percentage): It can represent either zero, one, or multiple characters with a variable length.
- _ (underscore): It is used to match only single character of a fixed length.
Similar Reads
SQL SELECT WHERE Field Contains Words
In SQL, the SELECT WHERE clause is a fundamental tool for filtering data based on specific conditions. When working with text fields, the SELECT WHERE clause helps identif...
15+ min read
MySQL | Regular Expressions (Regexp)
In MySQL, regular expressions (REGEX) are a powerful tool used to perform flexible pattern matching within string data. By utilizing REGEXP and RLIKE operators, developers...
15+ min read
Joining 4 Tables in SQL
The purpose of this article is to make a simple program to Join two tables using Join and Where clause in SQL. Below is the implementation for the same using MySQL. The pr...
15+ min read
SQL Query to Convert an Integer to Year Month and Days
With this article, we will be knowing how to convert an integer to Year, Month, Days from an integer value. The prerequisites of this article are you should be having a MS...
11 min read
SQL Auto Increment
In SQL databases, a primary key is important for uniquely identifying records in a table. However, sometimes it is not practical to manually assign unique values for each...
15+ min read
Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
Structured Query Language (SQL): Structured Query Language (SQL) has a specific design motive for defining, accessing and changement of data. It is considered as non-proce...
14 min read
How to Install MySQLdb module for Python in Linux?
In this article, we are discussing how to connect to the MySQL database module for python in Linux. MySQLdb is an interface for connecting to a MySQL database server from...
11 min read
SQL Query to Compare Two Strings
SQL stands for Structured Query Language. It is used to communicate with the database. There are some standard SQL commands like 'select', 'delete', 'alter' etc. To compar...
2 min read
SQL Query to Get Only Numbers From a String
As we know in an SQL database we can insert any type of data. Sometimes in the productions server, the data gets corrupted by two or more rows being merged and being saved...
2 min read
SQL Statement to Remove Part of a String
Here we will see SQL statements to remove part of the string. Method 1: Using SUBSTRING() and LEN() functionWe will use this method if we want to remove a part of the stri...
4 min read