SQL Query to Match Any Part of String
SQL Pattern Matching :
It is used for searching a string or a sub-string to find certain character or group of characters from a string. We can use LIKE Operator of SQL to search sub-string. The LIKE operator is used with the WHERE Clause to search a pattern in string of column. The LIKE operator is used in a conjunction with the two wildcards characters.
- Percentage sign( % ) : It represents zero, one or multiple characters of variable length.
- Underscore ( _ ) : It represents one, single character of fixed length.
Example :
In this example we will create a schema for our database and named it as geeksforgeeks. After that we will create a table inside it with the name geeks_data and tried to search a sub string from the data of table.
Step 1: Create a database :
In order to create a database we need to use the CREATE operator.
CREATE DATABASE geeksforgeeks;
Step 2: Create a table inside the database :
In this step we will create the table geeks_data inside the geeksforgeeks database.
CREATE TABLE geeksforgeeks.geeks_data(id INT, first_name VARCHAR(255), last_name VARCHAR(255), text_description VARCHAR(255), 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 geeksforgeeks.geeks_data (id, first_name, last_name, text_description) VALUES (1, "Rahul", "Khanna", "I am a backend developer who also like to technical content writing");
id | first_name | last_name | text_description |
1 | Rahul | Khanna | I am a backend developer who also like to technical content writing |
Step 4: Searching the pattern using Like operator :
SELECT first_name FROM geeksforgeeks.geeks_data WHERE text_description LIKE '%backend%developer%';
Step 5: Output :
We will get the first_name where in the description backend developer is present.
first_name |
Rahul |
Please Login to comment...