Sub topic 8
Nesting REPLACE to Clean Up Several Characters
When one unwanted character must be removed, REPLACE is simple. Real input often contains several unwanted characters. A phone number may contain parentheses, spaces, and hyphens. A product code may contain punctuation and inconsistent separators.
You can nest REPLACE calls so that the result of one call becomes the input to the next call. This chapter assumes you know basic SELECT statements, VARCHAR2, string literals, and the DUAL table.
How nested REPLACE calls work
REPLACE has this form:
REPLACE(source_text, search_text, replacement_text)It returns source_text after replacing every occurrence of search_text. If you omit replacement_text, Oracle removes the matching text.
Nested calls are evaluated from the inside outward. In this expression, the inner call runs first:
REPLACE(REPLACE(source_text, '(', ''), ')', '')The first call removes every opening parenthesis. The second call removes every closing parenthesis from that result.
Removing several characters
Here is a cleanup expression for parentheses and spaces:
SELECT REPLACE(
REPLACE(
REPLACE('(555) 123-4567', '(', ''),
')',
''
),
' ',
''
) AS cleaned
FROM dual;The result is:
555123-4567The hyphen remains because the expression never removes it. To remove the hyphen too, add another outer call:
SELECT REPLACE(
REPLACE(
REPLACE(
REPLACE('(555) 123-4567', '(', ''),
')',
''
),
' ',
''
),
'-',
''
) AS digits_only
FROM dual;The result is 5551234567.
A useful way to read a long expression is to format each operation on its own level:
REPLACE(
REPLACE(
REPLACE(text_value, '(', ''),
')',
''
),
' ',
''
)The formatting does not change the result. It makes the order visible and reduces mistakes.
Replacement order matters
The order matters when one replacement creates text that a later replacement also matches. For example:
SELECT REPLACE(
REPLACE('A-B', '-', '/'),
'/',
':'
) AS result
FROM dual;The first call changes the hyphen to a slash. The second call then changes that newly created slash to a colon. The result is A:B.
If the replacement strings do not overlap, the order usually does not matter. Still, write the calls in a clear, intentional order. Perform broad cleanup after specific cleanup when a later operation could affect text created earlier.
Removing versus changing text
A third argument of '' removes matches. A nonempty third argument changes them:
SELECT REPLACE('2026-11-28', '-', '/') AS display_date
FROM dual;This returns 2026/11/28.
Oracle treats an empty string literal as NULL in character data. For REPLACE, a NULL replacement removes the matching text. Therefore, these forms have the same practical purpose:
REPLACE(value, '-', '')
REPLACE(value, '-', NULL)Use '' when you want the intent to be obvious.
Choosing nested REPLACE
Nested calls work well when the cleanup rules are short and fixed. They are easy to use in a SELECT, a WHERE expression, or a derived value in an ORDER BY.
For many character substitutions, TRANSLATE can be shorter because it maps individual characters in one call. For patterns, optional text, or repeated groups, use REGEXP_REPLACE instead. Nested REPLACE is usually easier to read when each rule has a different replacement or when you want explicit control over the order.
Exercise
What is the result of REPLACE(REPLACE('a-b-c', '-', '/'), '/', '_')?
Show answer
a_b_c. The inner call changes hyphens to slashes, and the outer call changes those slashes to underscores.