EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Oracle Tutorial Oracle LIKE Operator
Secondary Sidebar
Oracle Tutorial
  • Advanced
    • Timestamp to Date in Oracle
    • Oracle Golden Gate
    • Oracle Virtual Machine
    • Oracle Describe Table
    • Oracle Clauses
    • Oracle Having Clause
    • Oracle?Primavera
    • Oracle FOREIGN Key
    • PIVOT in Oracle
    • Oracle Alter Table
    • Oracle Queries
    • Oracle Views
    • Oracle Window Functions
    • Oracle String Functions
    • Oracle Date Functions
    • Oracle Analytic Functions
    • Oracle Aggregate Functions
    • Select in Oracle
    • INSERT in Oracle
    • DISTINCT in Oracle
    • Function in Oracle
    • Oracle GROUP_CONCAT
    • Oracle INSTR()
    • Oracle CONVERT
    • Oracle LENGTH()
    • Oracle EXISTS
    • Oracle REPLACE()
    • Oracle MERGE
    • Oracle LEAD()
    • Oracle EXTRACT()
    • Oracle LISTAGG()
    • Oracle SYS_CONTEXT()
    • Oracle COALESCE
    • Oracle NVL()
    • Oracle SYSDATE()
    • Oracle?Date Format
    • Oracle SYS_GUID()
    • Oracle WILDCARDS
    • Oracle Synonyms
    • Oracle Subquery
    • BETWEEN in Oracle
    • FETCH in Oracle
    • Oracle Index
    • Oracle Function-based Index
    • Oracle UNIQUE Index
    • Oracle Bitmap Index
    • Oracle Column
    • Oracle Triggers
    • Oracle Procedures
    • Sample Database for Oracle
    • Oracle LIKE Operator
    • ORDER BY in Oracle
    • Oracle ORDER BY DESC
    • GROUP BY in Oracle
    • Oracle GROUP BY HAVING
    • Oracle Aliases
    • Table in Oracle
    • Oracle Temporary Table
    • Oracle? Table Partition
    • Oracle rename table
    • Oracle CTE
    • Cursor in Oracle
    • Oracle LOCK TABLE
    • Oracle Tablespace
    • Oracle CARDINALITY
    • Oracle REGEXP
    • Oracle REGEXP_REPLACE
    • Oracle to_date
    • JSON in Oracle
    • Oracle COMMIT
    • Oracle GRANT
    • Oracle MD5
    • Oracle ROLLBACK
    • Oracle Users
    • Oracle TIMESTAMP
    • IF THEN ELSE in Oracle
    • Oracle While Loop
    • Oracle Clone Database
    • Oracle Backup Database
    • Oracle? XML
    • Oracle XMLAGG
    • Oracle XMLTABLE
    • Oracle Performance Tuning
    • Oracle B Tree Index
    • Oracle fusion
    • Oracle ebs
    • Oracle GRC
    • Oracle ERP
    • Oracle ASM
    • Oracle Cloud
    • Oracle HCM Cloud
    • Oracle Integration Cloud
    • Oracle Jinitiator
    • Oracle pathfinder
    • Oracle VirtualBox
    • Oracle Weblogic Server
    • Oracle decode
    • Oracle Exadata
    • Oracle ZFS
    • Oracle? utilities
    • JDBC Driver for Oracle
    • Oracle? DBA Versions
    • Oracle DBA Salary
  • Basic
    • Oracle Marketing Cloud
    • What is Oracle?
    • Career in Oracle
    • How to Install Oracle
    • Oracle Versions
    • What Is Oracle Database
    • Oracle Data Warehousing
    • Oracle Warehouse Builder
    • Career In Oracle Database Administrator
    • Career In Oracle DBA
    • What is Oracle RAC
    • Oracle DBA
    • Oracle? Vanderbilt
    • What is RMAN Oracle
    • Oracle Database Administration
    • Oracle Operators
    • Oracle Constraints
    • Oracle number
    • Oracle Data Types
    • Oracle UNIQUE Constraint
    • Oracle Check Constraint
  • Joins
    • Joins in Oracle
    • Inner Join in Oracle
    • Oracle Cross Join
    • Left Join in Oracle
    • OUTER Join in Oracle
    • Oracle Full Outer Join
    • Natural Join in Oracle
    • Oracle Self Join
    • Oracle hash join
    • Oracle? Update with Join
  • Oracle SET Operators
    • UNION in Oracle
    • Oracle UNION ALL
    • INTERSECT in Oracle
    • MINUS in Oracle
  • Interview Questions
    • Oracle Interview Questions
    • Oracle Apps Interview Questions
    • Oracle Apps Technical Interview Questions
    • Oracle Database Interview Questions
    • Oracle Forms Interview Questions
    • Oracle PL/SQL Interview Questions
    • Oracle RAC Interview Questions
    • Oracle SOA Interview Questions

Related Courses

Oracle Course Training

Oracle DBA Certification Course

MongoDB Certification Training

Oracle LIKE Operator

By Priya PedamkarPriya Pedamkar

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.

The available WILD CARDS in Oracle are :

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (85,992 ratings)
  • % : 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
Popular Course in this category
Oracle Training (14 Courses, 8+ Projects)
  14 Online Courses |  8 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle DBA Database Management System Training (2 Courses)4.9
All in One Financial Analyst Bundle- 250+ Courses, 40+ Projects4.8
1 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more