Updated March 23, 2023
Introduction to Oracle LIKE Operator
LIKE Operator in Oracle database does pattern matching as LIKE condition matches the portion of one character value with another by searching the first value for the pattern (provided in the SQL query as LIKE operator allows the wildcards to be used with WHERE clause of SELECT, INSERT, UPDATE or DELETE statement) specified by the second similarly it calculates strings using characters as they are defined by the input character set.
The available WILD CARDS in Oracle are :
- % : Used to present any sequence of zero or more character
- _ : Represents any single character, only at that position
Syntax:
SELECT …….FROM TableName WHERE ColumnName/Expression LIKE character/pattern/literal.
Example:
SELECT col_1, col_2…..col_n FROM TableName WHERE ColumnName LIKE ‘pat%’;
Implementation of Oracle LIKE Operator
We will use the below table with 14 records throughout the examples to understand the LIKE operator behavior.
Query:
SELECT ename, job, hiredate, sal from Emp;
Output:
1. LIKE operator in WHERE clause
Query:
SELECT ename, job, hiredate, sal FROM emp WHERE ename LIKE ‘KI%’;
Output:
In this SQL SELECT statement LIKE operator search for the entry which begins with character pattern KI in ename column and fetch the match pattern. The SQL statement fetched ‘KING’ ename with details because in ename column only KING starts with KI character.% represents any character pattern from zero count to any number of character counts.
2. LIKE operator in WHERE clause with NOT keyword
Query:
SELECT ename, job, hiredate, sal FROM emp WHERE ename NOT LIKE ‘KI%’;
Output:
In this output, KING’s records are missing. Because there is NOT keyword with the LIKE operator, NOT behaves like negation means this SQL statement selects only records which don’t start with the supplied pattern ‘KI’ in LIKE operator.
3. LIKE operator with underscore( ‘_’) WILD CARD search
Query:
SELECT ename, job, hiredate, sal from Emp WHERE ename LIKE ‘_A%’
Output:
In this example we used underscore (‘_’) WILD CARD search, it represents any single character. This statement selects the entries in ename which is having second character A, but the first character can be anything.
4. LIKE operator with % WILD CARD search
Query:
SELECT ename, job, hiredate, sal from Emp where ename LIKE ‘%A%’;
Output:
In this example, the LIKE operator selects the entries from the ename column which consists of ‘A’ character at any position and displays the records.
5. LIKE operator with any DATA TYPE column
We can apply a LIKE operator on any data type column as explained below:
Example #1
We can apply a LIKE operator for ename column which is a VARCHAR2 (character) data type.
Query:
SELECT ename, job, hiredate, sal FROM Emp where ename LIKE ‘%A%’;
Output:
Example #2
We can apply a LIKE operator on the sal column which is a NUMBER data type.
Query:
SELECT ename, job, hiredate, sal FROM Emp where sal LIKE ‘30%’;
Output:
Example #3
We can apply a LIKE operator on the hiredate column which is a DATE data type.
Query:
SELECT ename, job, hiredate, sal FROM Emp where hiredate LIKE ‘%MAY%’;
Output:
Oracle LIKE Operator with ESCAPE Clause
We will use the below table with 08 records as a reference for the examples to understand the LIKE operator behavior with the ESCAPE option more deeply.
Example #1
Here we are selecting all data using the escape clause given below.
Query:
SELECT * FROM Dept;
Output:
Example #2
Here we are using the %_% to access data which is given below.
Query:
SELECT deptno, dname, loc FROM Dept WHERE dname LIKE ‘%_%’;
Output:
Example #3
This is the %\% operator using the escape clause which is explained below.
Query:
SELECT deptno, dname, loc FROM Dept WHERE dname LIKE ‘%\%’;
Output:
If we see the above two examples, we find a few points that need attention
- For example 1 the SQL statement to get the records of dname which consist underscore ‘_’ but we got all the records instead of that specific search record.
- But in example 2 the SQL statement to get the records of dname which consist backslash ‘\’ and got the correct result.
Here two questions arise:
1. Why this issue occurred when we used the correct syntax?
Answer: In example 1 we used “LIKE ‘%_%’. As we know there are two WILD CARDS in ORACLE i.e. ‘%’ & ‘_’. In that example underscore “_” behaves as a WILD CARD, Not as a character. That’s why the issue occurred.
2. How to fix this problem?
Answer: For finding the exact pattern match for ‘%’ and ‘_’, we use the ESCAPE option along with ‘\’. It helps us to find the exact match. Some examples below.
Examples of ESCAPE Clause
Here are some of the examples of the escape clause which are explained below:
Example #1
We can use ESCAPE to make underscore ‘_’ as a character.
Query:
SELECT deptno, dname, loc FROM Dept WHERE dname LIKE ‘%\_%’ ESCAPE ‘\’;
Output:
Example #2
We can use % as a character using the ESCAPE option.
Query:
SELECT deptno, dname, loc FROM Dept WHERE dname LIKE ‘%\%%’ ESCAPE ‘\’;
Output:
Example #3
We can use a combination of the Oracle UPPER function and the LIKE condition to return all of the records where the dname field contains the word “%RA”, regardless of whether it was stored as %RA, %Ra, or %ra.
Query:
SELECT deptno, dname, loc FROM Dept WHERE UPPER (dname) LIKE UPPER ('%\%ra%') ESCAPE '\';
Output:
Tip: We can supply LOWER and INITCAP function instead of UPPER as well for case sensitive search.
Example #4
We can use @, $, &, ^, ? etc. keyword instead of ‘\’ with escape option to get the exact match. Few examples below.
Query 1:
SELECT deptno, dname, loc FROM Dept WHERE LOWER (dname) LIKE ('%^%ra%') ESCAPE '^';
Output:
Query 2:
SELECT deptno, dname, loc FROM Dept WHERE LOWER (dname) LIKE ('%@%ra%') ESCAPE '@';
Output:
Example #5
We can use a logical operator to perform multiple LIKE conditions in a single SQL statement.
Query:
SQL> SELECT deptno, dname, loc from Dept WHERE LOWER(dname) LIKE ('%^%ra%') ESCAPE '^' OR LOWER(dname) LIKE ('%!_de%') ESCAPE '!';
Output:
Conclusion
Sometimes, we need to query data but we don’t know the exact/full pattern/content of that column then LIKE operator is the best option to pattern search. For example, if we may want to find dname which ends with ‘DECK’ or starts with ‘cargo’ LIKE operator is very useful in those scenarios.
Recommended Articles
This is a guide to Oracle LIKE Operator. Here we discuss the implementation and the concept of escape clause with Oracle LIKE operator along with its examples. You can also go through our other suggested articles to learn more –