Updated March 31, 2023
Introduction to PL/SQL LIKE
PL/SQL provides the different types of functionality to the user, basically like is one type of functionality that the PL/SQL provides. By using the like functionality or clause, we can match the specified pattern as per our requirement. In another word, we can say that like is a pattern matching operator. Like is used to compare different strings with the respective pattern and perform the wildcard searches as per the user requirement. One more important point is that the like operator only works on the character string. If the string character matches with the pattern, then it returns the result; otherwise, it returns the false value.
Syntax:
Specified string variable LIKE pattern
Explanation:
- In the above syntax, LIKE operator for pattern matching, here specified string variable is used to represent any variable that contains the string, whether string may contain varchar or char. After that, we use a pattern; the pattern also contains the string or string literal.
- The LIKE operator uses the two wildcard characters: percentage sign (%) and underscore (_).
How LIKE work in PL/SQL?
Given below shows how the LIKE pattern matching operator works in PL/SQL:
You may compare a value to a pattern rather than a constant using the LIKE criteria. After the LIKE keyword, the pattern must occur.
UNLIKE THE EQUALITY OPERATOR (=), the LIKE criteria describe a pattern-matching test, unlike the equality operator (=), which matches a portion of one character value to another by searching the first value for the pattern specified by the second value. LIKE creates strings from the characters in the input character set.
Basically, the like operator contains two special characters to match the pattern that we also called a wildcat character as follows:
- In a multi-byte character set, an underscore (_) in the pattern matches exactly one character (as opposed to one byte).
- A percent sign (%) in the pattern can match zero or more characters (as opposed to bytes) in the value in a multi-byte character set. A null cannot be matched by the pattern ‘% ‘.
- If we need to match the exact pattern, then we can use / for exactly one.
When we execute the LIKE statement in PL/SQL at that time, Oracle database the given pattern into the sub-patterns that contain one or more characters.
For example, let’s consider we have A1, A2, ……., An sub-patterns and LIKE condition is true if there is a path to make substring S1, S2,…, Sn by using value, that means all substring between the 1 and n, and it has the following condition as follows:
- If Ai contains the underscore (_), then substring Si has a single character.
- If Ai contains the percentage sign (%), then substring Si has any character.
- If Ai consists of two characters, the first of which is an escape character, then Si is Ai’s second character.
- Otherwise, Ai is equal to Si.
LIKE operator is case sensitivity:
When comparing character expressions that employ the LIKE condition and the equality (=) operators, case matters in every case. The NLS SORT and NLS COMP session options can be used to execute case or accent insensitive LIKE searches.
Examples of PL/SQL LIKE
Given below are the examples of PL/SQL LIKE:
Example #1
Code:
CREATE OR REPLACE FUNCTION userphone (contact_no IN VARCHAR2)
RETURN VARCHAR2 AS
BEGIN
IF contact_no LIKE '____-___-____-____' THEN
RETURN SUBSTR(contact_no,1,4);
ELSE
RETURN 'none';
END IF;
END;
/
Explanation:
- In the above example, we use create function statement; here, we created a user phone function with contact_no variable as shown in the above example. Here we use the varchar2 data type. Then, inside the execution section, we write an if statement with the LIKE operator to match the specified contact number.
- If the specified contact number matches, it returns the same number, and if a specified contact number does not match, it returns none.
Output:
After the successful creation of a function, we need to insert the value by using the following statement as follows.
Code:
select userphone(123145-111-22222-44545) from dual;
Explanation:
- In the above statement, we use a select statement to verify the contact number pattern. In this example, we pass the contact number with a given format that we already specified in the function with the help of the LIKE operator.
- The final output of the above procedure we illustrated by using the following screenshot as follows.
Output:
Example #2
Another example of the LIKE operator is as follows.
First, create a table by using the following statement as follows.
Code:
create table sample_user(user_id int, user_name varchar(30), user_city varchar(20));
Explanation:
- By using the above statement, we created a sample_user table with different attributes such as user_id, user_name, and user_city with different data types as shown. The final output of the above procedure we illustrated by using the following screenshot as follows.
Output:
Now insert some records into the sample_user table by using the following statement as follows.
Code:
insert into sample_user(user_id, user_name, user_city) values(1, 'Jenny', 'Hongkong');
select * from sample_user;
Explanation:
- By using the above statement, we insert records into the sample_user table. For example, here, we inserted three records as shown below screenshot as follows.
Output:
Now we can use the LIKE operator as per our requirement as follows.
Suppose we need to find whose user name starts with the J character at that time; we can use the following statement as follows.
Code:
select * from sample_user where user_name like 'J%'order by user_name;
Explanation:
- In the above statement, we use the select and where clause to match the pattern; here, we need to find the user name with the J character.
- The final output of the above procedure we illustrated by using the following screenshot as follows.
Output:
Similarly, we can perform different operations by using LIKE as per our requirements.
Conclusion
From the above article, we have seen the basic syntax of like, and we also saw different examples of the like. From this article, we have seen how and when we use PL/SQL LIKE.
Recommended Articles
We hope that this EDUCBA information on “PL/SQL LIKE” was beneficial to you. You can view EDUCBA’s recommended articles for more information.