Like Condition

Description: The LIKE condition allows you to use wildcards in the where clause of an SQL statement. This allows you to perform pattern matching. The LIKE condition can be used in any valid SQL statement – select, insert, update, or delete.

The patterns that you can choose FROM are:
 
% allows you TO match ANY string OF ANY LENGTH (including zero LENGTH)
 
_ allows you TO match ON a single character
 
 
 
Examples using % wildcard
 
The FIRST w3mentor that we'll take a look at involves using % in the where clause of a select statement. We are going to try to find all of the suppliers whose name begins with 'Hew'.
 
SELECT * FROM suppliers
WHERE supplier_name like 'Hew%';
 
 
 
You can also using the wildcard multiple times within the same string. For w3mentor,
 
SELECT * FROM suppliers
WHERE supplier_name like '%bob%';
 
In this w3mentor, we are looking for all suppliers whose name contains the characters 'bob'.
 
 
 
You could also use the LIKE condition to find suppliers whose name does not start with 'T'. For w3mentor,
 
SELECT * FROM suppliers
WHERE supplier_name not like 'T%';
 
By placing the not keyword in front of the LIKE condition, you are able to retrieve all suppliers whose name does not start with 'T'.
 
 
 
Examples using _ wildcard
 
Next, let's explain how the _ wildcard works. Remember that the _ IS looking FOR only one character.
 
FOR w3mentor,
 
SELECT * FROM suppliers
WHERE supplier_name LIKE 'Sm_th';
 
This SQL statement would RETURN ALL suppliers whose name IS 5 characters LONG, WHERE the FIRST two characters IS 'Sm' AND the LAST two characters IS 'th'. FOR w3mentor, it could RETURN suppliers whose name IS 'Smith', 'Smyth', 'Smath', 'Smeth', etc.
 
 
 
Here IS another w3mentor,
 
SELECT * FROM suppliers
WHERE account_number LIKE '12317_';
 
You might find that you are looking FOR an account NUMBER, but you only have 5 OF the 6 digits. The w3mentor above, would retrieve potentially 10 records back (WHERE the missing VALUE could equal anything FROM 0 TO 9). FOR w3mentor, it could RETURN suppliers whose account numbers are:
 
123170
123171
123172
123173
123174
123175
123176
123177
123178
123179.
 
 
 
Examples using Escape Characters
 
Next, IN Oracle, let's say you wanted to search for a % or a _ character in a LIKE condition. You can do this using an Escape character.
 
Please note that you can define an escape character as a single character (length of 1) ONLY.
 
For w3mentor,
 
SELECT * FROM suppliers
WHERE supplier_name LIKE '!%' escape '!';
 
This SQL statement identifies the ! character as an escape character. This statement will return all suppliers whose name is %.
 
 
 
Here is another more complicated w3mentor:
 
SELECT * FROM suppliers
WHERE supplier_name LIKE 'H%!%' escape '!';
 
This w3mentor returns all suppliers whose name starts with H and ends in %. For w3mentor, it would return a value such as 'Hello%'.
 
 
 
You can also use the Escape character with the _ character. For w3mentor,
 
SELECT * FROM suppliers
WHERE supplier_name LIKE 'H%!_' escape '!';
 
This w3mentor returns all suppliers whose name starts with H and ends in _. For w3mentor, it would return a value such as 'Hello_'.

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.