EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Oracle Tutorial Oracle LIKE Operator
 

Oracle LIKE Operator

Priya Pedamkar
Article byPriya Pedamkar

Updated March 23, 2023

Oracle-Like-Operator

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

oracle like eg1

1. LIKE operator in WHERE clause

Query:

SELECT ename, job, hiredate, sal FROM emp WHERE ename LIKE ‘KI%’;

Output:

like operator in where

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:

like operator in where not

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:

like operator in underscore

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:

like operator WILD CARD

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:

like eg1

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:

like eg2

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:

like eg3

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:

ESCAPE Clause

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:

ESCAPE Clause 1

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:

ESCAPE Clause 2

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:

escape eg1

Note: In this example, we used an ESCAPE to make underscore ‘_’ as a character. And got the exact match.

Example #2

We can use % as a character using the ESCAPE option.

Query:

SELECT deptno, dname, loc FROM Dept WHERE dname LIKE ‘%\%%’ ESCAPE ‘\’;

Output:

ESCAPE Clause 3

Note: The above example shows that WILD CARD ‘%’ (second from the left) behaved like a character because of the ESCAPE option.

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:

Oracle LIKE Operator 3

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:

Oracle LIKE Operator 3

Query 2:

SELECT deptno, dname, loc FROM Dept WHERE LOWER (dname) LIKE ('%@%ra%') ESCAPE '@';

Output:

Oracle LIKE Operator3

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:

Oracle LIKE Operator 5

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 –

  1. Operations in OLAP
  2. Comparison Operators in JavaScript
  3. Unix Operators
  4. C# OR Operator
  5. Top Versions of Oracle
  6. Guide to Comparison Operators in Java
  7. Top 7 Oracle Operators with Examples

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW