In SQL to filter data based on a specific pattern, the LIKE clause is used in the WHERE condition. It can be applied to date, string, or numeric values. However, using the string values is advised.
The following are some examples of how the LIKE clause is used:
The LIKE operator in SQL supports two wildcard characters:
Syntax:
SELECT column1, column2, …
FROM table_name
WHERE columnN LIKE pattern;
Consider the following Product table.
ID | ITEM | PRICE |
---|---|---|
101 | Jar | 250.00 |
102 | Basketball | 500.00 |
103 | Chair | 2500.00 |
The following table has a few examples showing the WHERE part having different LIKE clause with ‘%’ and ‘_’ operators −
SELECT * FROM Product WHERE PRICE LIKE 'XXX';
Statement | Description |
---|---|
WHERE PRICE LIKE ‘25%’ | Finds any values that start with 25. |
WHERE PRICE LIKE ‘%00%‘ | Finds any values that have 00 in any position. |
WHERE PRICE LIKE ‘_00%’ | Finds any values that have 00 in the second and third positions. |
WHERE PRICE LIKE ‘2_%_%‘ | Finds any values that start with 2 and are at least 3 characters in length. |
WHERE PRICE LIKE ‘%2’ | Finds any values that end with 2. |
WHERE PRICE LIKE ‘_5%0‘ | Finds any values that have a 5 in the second position and end with a 0. |
Outputs will be generated as per the input query.
Note: also read about SQL: WHERE clause
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…
Problem Statement (Asked By Airbnb) Given a list of integers, write a function to compute…