Pattern Matching in PL/SQL โ LIKE and REGEXP Examples
Pattern Matching in PL/SQL is a technique used to search strings that follow specific patterns. It allows developers to retrieve data not by exact value but by how it matches a formโeither partially, loosely, or precisely. Oracle supports two main methods for this:
LIKE operator โ traditional, wildcard-based matching
REGEXP_LIKE โ advanced matching using regular expressions
Introduction to Pattern Matching in PL/SQL
In PL/SQL, pattern matching refers to evaluating text columns using partial or flexible string criteria. This is useful in scenarios like:
Finding all names starting with “J”
Filtering data that contains specific characters
Matching specific structured values (e.g., IDs, codes, etc.)
Using the LIKE Operator for Pattern Matching in PL/SQL
The LIKE
operator allows for pattern matching in the WHERE
clause using two wildcards:
%
: Matches any number of characters_
: Matches a single character
SELECT column1
FROM table_name
WHERE column1 LIKE 'pattern';
Example: Pattern Matching with LIKE
CREATE OR REPLACE PROCEDURE author_sel (
i_last_name IN AUTHORS.LAST_NAME%TYPE,
cv_author OUT SYS_REFCURSOR
)
IS
v_last_name AUTHORS.LAST_NAME%TYPE;
BEGIN
v_last_name := '%' || UPPER(i_last_name) || '%';
OPEN cv_author FOR
SELECT id, first_name, last_name
FROM authors
WHERE UPPER(last_name) LIKE v_last_name;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
VARIABLE x REFCURSOR
EXEC author_sel('rin', :x)
PRINT x
28 Sumit Sarin
54 Cheryl Riniker
REGEXP_LIKE Function for Pattern Matching in PL/SQL
REGEXP_LIKE
is a function introduced in Oracle 10g. It offers powerful pattern matching using regular expressions, which support complex criteria far beyond what LIKE allows.
REGEXP_LIKE(column, pattern [, match_parameter])
column
: column to be searchedpattern
: regular expression stringmatch_parameter
: optional flags (i
for case-insensitive)
Common Regular Expression Metacharacters
Character | Description |
---|---|
* | Matches zero or more characters |
. | Matches any single character |
^ | Matches the start of a string |
$ | Matches the end of a string |
[] | Defines a set or range of characters |
` | ` |
() | Groups expressions |
\ | Escape character |
Example: REGEXP_LIKE in a PL/SQL Procedure
CREATE OR REPLACE PROCEDURE author_sel_regexp_like (
cv_author OUT SYS_REFCURSOR
)
IS
BEGIN
OPEN cv_author FOR
SELECT id, first_name, last_name
FROM authors
WHERE REGEXP_LIKE(last_name, '^har(d|t)man$', 'i');
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
This matches:
Hardman
Hartman
Ignores case
VARIABLE x REFCURSOR
EXEC author_sel_regexp_like(:x)
PRINT x
44 Ron Hardman
55 Robert Hartman
Best Practices
Use
LIKE
for simple pattern matching on short strings.Use
REGEXP_LIKE
for complex or structured pattern criteria.Always normalize data (e.g., use
UPPER()
) for consistent matching.Escape special characters in user input for REGEXP security.
Use indexed columns carefully with pattern matchingโperformance may degrade.
AmantPoint Exclusive Learning Series
ยฉ 2025 AmantPoint. All rights reserved. Educational content for developers and learners.