Oracle LIKE Operator: Querying data Based on a Specified Pattern (2024)

Summary: in this tutorial, you will learn how to use the Oracle LIKE operator to test whether values in a column match a specified pattern.

Introduction to the Oracle LIKE operator

Sometimes, you want to query data based on a specified pattern. For example, you may want to find contacts whose last names start with 'St' or first names end with 'er'. In this case, you use the Oracle LIKE operator.

The syntax of the Oracle LIKE operator is as follows:

expresion [NOT] LIKE pattern [ ESCAPE escape_characters ] Code language: CSS (css)

In this syntax, we have:

1) expression

The expression is a column name or an expression that you want to test against the pattern.

2) pattern

The pattern is a string to search for in the expression. The pattern includes the following wildcard characters:

  • % (percent) matches any string of zero or more characters.
  • _ (underscore) matches any single character.

3) escape_character

The escape_character is a character that appears in front of a wildcard character to specify that the wildcard should not be interpreted as a wildcard but as a regular character.

The escape_character, if specified, must be one character and it has no default value.

The LIKE operator returns true if the expression matches the pattern. Otherwise, it returns false.

The NOT operator, if specified, negates the result of the LIKE operator.

Oracle LIKE examples

Let’s take some examples of using the Oracle LIKE operator to see how it works.

We will use the contacts table in the sample database for the demonstration:

Oracle LIKE Operator: Querying data Based on a Specified Pattern (1)

A) % wildcard character examples

The following example uses the % wildcard to find the phones of contacts whose last names start with 'St':

SELECT first_name, last_name, phoneFROM contactsWHERE last_name LIKE 'St%'ORDER BY last_name;Code language: JavaScript (javascript)

The following picture illustrates the result:

Oracle LIKE Operator: Querying data Based on a Specified Pattern (2)

In this example, we used the pattern:

'St%'Code language: JavaScript (javascript)

The LIKE operator matched any string that starts with 'St' and is followed by any number of characters e.g., Stokes, Stein, or Steele, etc.

To find the phone numbers of contacts whose last names end with the string 'er', you use the following statement:

SELECT first_name, last_name, phoneFROM contactsWHERE last_name LIKE '%er'ORDER BY last_name;Code language: JavaScript (javascript)

Here is the result:

Oracle LIKE Operator: Querying data Based on a Specified Pattern (3)

The pattern:

%er

matches any string that ends with the 'er' string.

To perform a case-insensitive match, you use either LOWER() or UPPER() function as follows:

UPPER( last_name ) LIKE 'ST%'LOWER(last_name LIKE 'st%'Code language: JavaScript (javascript)

For example, the following statement finds emails of contacts whose first names start with CH:

SELECT first_name, last_name, emailFROM contactsWHERE UPPER( first_name ) LIKE 'CH%';ORDER BY first_name;Code language: SQL (Structured Query Language) (sql)

Here is the result:

Oracle LIKE Operator: Querying data Based on a Specified Pattern (4)

The following example uses the NOT LIKE operator to find contacts whose phone numbers do not start with '+1':

SELECT first_name, last_name, phoneFROM contactsWHERE phone NOT LIKE '+1%'ORDER BY first_name;Code language: JavaScript (javascript)

The result is:

Oracle LIKE Operator: Querying data Based on a Specified Pattern (5)

B) _ wildcard character examples

The following example finds the phone numbers and emails of contacts whose first names have the following pattern 'Je_i':

SELECT first_name, last_name, email, phoneFROM contactsWHERE first_name LIKE 'Je_i'ORDER BY first_name;Code language: SQL (Structured Query Language) (sql)

Here is the result:

Oracle LIKE Operator: Querying data Based on a Specified Pattern (6)

The pattern 'Je_i' matches any string that starts with 'Je', followed by one character, and then followed by 'i' e.g., Jeri or Jeni, but not Jenni.

C) Mixed wildcard characters example

You can mix the wildcard characters in a pattern. For example, the following statement finds contacts whose first names start with the string Je followed by two characters and then any number of characters. In other words, it will match any last name that starts with Je and has at least 3 characters:

SELECT first_name, last_name, email, phoneFROM contactsWHERE first_name LIKE 'Je_%';Code language: JavaScript (javascript)

Oracle LIKE Operator: Querying data Based on a Specified Pattern (7)

D) ESCAPE clause examples

The ESCAPE clause allows you to find strings that include one or more wildcard characters.

For example, a table may include data that has percent % character such as discount values, and depreciation rate.

To search for the string 25%, you use the ESCAPE clause as follows:

LIKE '%25!%%' ESCAPE '!'Code language: JavaScript (javascript)

If you don’t use the ESCAPE clause, Oracle will return any rows with the string 25.

The following statements create discounts table and insert some sample data for testing:

CREATE TABLE discounts ( product_id NUMBER, discount_message VARCHAR2( 255 ) NOT NULL, PRIMARY KEY( product_id ) );INSERT INTO discounts(product_id, discount_message)VALUES(1, 'Buy 1 and Get 25% OFF on 2nd ');INSERT INTO discounts(product_id, discount_message)VALUES(2, 'Buy 2 and Get 50% OFF on 3rd ');INSERT INTO discounts(product_id, discount_message)VALUES(3, 'Buy 3 Get 1 free');Code language: PHP (php)

If you are not familiar with the statements used in this script, you can learn them in the subsequent tutorials.

The following statement retrieves products that have a discount of 25%:

SELECTproduct_id,discount_messageFROMdiscountsWHEREdiscount_message LIKE '%25!%%' ESCAPE '!';Code language: SQL (Structured Query Language) (sql)

The result is as follows:

Oracle LIKE Operator: Querying data Based on a Specified Pattern (8)

In this tutorial, you have learned how to use the Oracle LIKE operator to query data that match a specified pattern.

Was this tutorial helpful?

Oracle LIKE Operator: Querying data Based on a Specified Pattern (2024)

FAQs

Which operator can be used in a query for pattern matching? ›

Like Operator is the Correct Answer. In SQL, the operator that performs pattern matching is the “LIKE” operator. The “LIKE” operator is used to match values in a column against a specific pattern, where the pattern can include wildcard characters to represent unknown or variable characters.

What does like %% mean in SQL? ›

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign % represents zero, one, or multiple characters. The underscore sign _ represents one, single character.

Which operator is used for pattern matching in Oracle? ›

The LIKE conditions specify a test involving pattern matching. Whereas the equality operator (=) exactly matches one character value to another, the LIKE conditions match a portion of one character value to another by searching the first value for the pattern specified by the second.

How to match pattern in Oracle SQL? ›

Pattern matching in SQL is performed using the MATCH_RECOGNIZE clause. MATCH_RECOGNIZE enables you to do the following tasks: Logically partition and order the data that is used in the MATCH_RECOGNIZE clause with its PARTITION BY and ORDER BY clauses.

Which operator is commonly used to SELECT data based on patterns? ›

The SQL LIKE operator searches for a specified pattern within a column. It is commonly used with the SELECT statement to filter rows based on a specific condition.

Which operator can be used to search for patterns? ›

The LIKE operator in SQL is used to search for patterns in strings. It is commonly used in the WHERE clause to filter data based on specific patterns. The LIKE operator utilizes wildcard characters to match various character combinations.

What is the like operator in pattern matching? ›

Like Operator and Pattern Matching. The like operator lets you compare two strings to see whether they resemble each other in specific ways. To use this operator, specify a pattern (a string of characters with special formatting characters) that specifies what the compared string must look like.

How to use like operator in dynamic SQL in Oracle? ›

How do I use a like operator in dynamic SQL in Oracle? Create pattern dynamically and use it in like statement : Like « Select Query « Oracle PL / SQL. SQL> SQL> SQL> CREATE TABLE emp ( 2 id NUMBER PRIMARY KEY, 3 fname VARCHAR2(50), 4 lname VARCHAR2(50) 5 ); Table created.

What is the difference between like and Ilike? ›

The LIKE expression matches case-sensitive string patterns, whereas the ILIKE expression matches case-insensitive string patterns. Both of these expressions provide: flexible string comparison using special characters.

How to use like operator in Oracle query? ›

The LIKE operator is written as a word “LIKE” followed by a pattern to match with column values. Here is an example of LIKE operators in Oracle, SELECT * FROM Employee WHERE name LIKE '%P'; Here the symbol percentage is a wildcard character.

How to give a pattern in SQL query? ›

SQL LIKE Pattern Matching Tutorial
  1. Use LIKE for Exact String Match.
  2. Use '%' to match any number of characters.
  3. Use '_' to match one (and only one) character.
  4. Use both '%' and '_' to match any pattern.
  5. Use NOT to find strings that do not match a pattern.
  6. Use LOWER (or UPPER) with LIKE for case-insensitive pattern matching.

Which operator is used to extract pattern matching data from the database? ›

LIKE operator is used for pattern matching, and it can be used as -. % – It matches zero or more characters.

Which operator in SQL perform pattern matching? ›

The LIKE operator, on the other hand, is specifically designed for pattern matching. It uses wildcard characters such as "%" and "_" to search for values that match a specified pattern. With the LIKE operator, you can perform pattern-based searches in SQL, making it the correct choice for this question.

Which command is used for pattern matching? ›

Pattern matching is used by the shell commands such as the ls command, whereas regular expressions are used to search for strings of text in a file by using commands, such as the grep command.

What is match_recognize in Oracle? ›

Recognizes matches of a pattern in a set of rows. MATCH_RECOGNIZE accepts a set of rows (from a table, view, subquery, or other source) as input, and returns all matches for a given row pattern within this set. The pattern is defined similarly to a regular expression.

Which operator performs pattern matching in Python? ›

LIKE operator is used for pattern matching, and it can be used as -. % – It matches zero or more characters.

Which between operator performs pattern matching? ›

The correct answer to the question “Which operator performs pattern matching in SQL” is option (b). LIKE operator.

Which keyword is used for pattern matching in SQL? ›

In SQL, the LIKE keyword is used to search for patterns. Pattern matching employs wildcard characters to match different combinations of characters. The LIKE keyword indicates that the following character string is a matching pattern. LIKE is used with character data.

References

Top Articles
Latest Posts
Article information

Author: Sen. Ignacio Ratke

Last Updated:

Views: 5497

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Sen. Ignacio Ratke

Birthday: 1999-05-27

Address: Apt. 171 8116 Bailey Via, Roberthaven, GA 58289

Phone: +2585395768220

Job: Lead Liaison

Hobby: Lockpicking, LARPing, Lego building, Lapidary, Macrame, Book restoration, Bodybuilding

Introduction: My name is Sen. Ignacio Ratke, I am a adventurous, zealous, outstanding, agreeable, precious, excited, gifted person who loves writing and wants to share my knowledge and understanding with you.