Sub topic 4
REPLACE: substituting and removing text
This chapter assumes you can write basic SELECT statements, use WHERE, and recognize VARCHAR2, string literals, DUAL, and NULL. Oracle's REPLACE function searches a character string for one literal substring and substitutes another string for every match.
Basic substitution
The syntax is:
REPLACE(source_string, search_string [, replacement_string])source_stringis the value to inspect.search_stringis the text to find.replacement_stringis the text to insert instead.- If
replacement_stringis omitted, Oracle removes every match.
REPLACE is case-sensitive. The search string 'a' does not match 'A'.
SELECT REPLACE('2026-11-28', '-', '/') AS slashed,
REPLACE('Oracle SQL', 'SQL', 'Database') AS changed
FROM dual;The results are 2026/11/28 and Oracle Database. Every occurrence is replaced, not only the first one.
The function treats its arguments as literal text. It does not interpret . or * as pattern characters. Use a regular-expression function when the replacement depends on a pattern rather than a fixed substring.
Removing text
Omit the third argument to remove matches:
SELECT REPLACE('555-123-4567', '-') AS digits_only
FROM dual;The result is 5551234567. This is useful for removing one known separator from a value.
You can also replace a match with an empty string literal:
SELECT REPLACE('A B C', ' ', '') AS compact_text
FROM dual;Oracle treats a zero-length character value as `NULL. In this use, the result is still the original text with spaces removed.
How replacement works
Oracle scans the source from left to right. It replaces non-overlapping matches. The replacement text is not scanned again during the same call.
For example:
SELECT REPLACE('aaaa', 'aa', 'b') AS result
FROM dual;The result is bb. The two aa matches do not overlap.
If the search string does not occur, Oracle returns the original source value:
SELECT REPLACE('Oracle', 'Java', 'SQL') AS unchanged
FROM dual;The result is Oracle.
The following table summarizes common outcomes.
| Expression | Result | Reason |
|---|---|---|
| `REPLACE('a-b-c', '-', '/')` | `a/b/c` | Every hyphen is replaced |
| `REPLACE('a-b-c', '-')` | `abc` | Every hyphen is removed |
| `REPLACE('Oracle', 'x', 'y')` | `Oracle` | The search text is absent |
| `REPLACE(NULL, 'x', 'y')` | `NULL` | A `NULL` source produces `NULL` |
Nesting REPLACE calls
To remove or substitute several different characters, nest calls. The inner call runs first:
SELECT REPLACE(
REPLACE(
REPLACE('(555) 123-4567', '(', ''),
')', ''),
' ', '') AS cleaned
FROM dual;The result is 555123-4567. The parentheses and space are removed, but the hyphen remains because no call removes it.
To remove the hyphen too, add another call:
SELECT REPLACE(
REPLACE(
REPLACE(
REPLACE('(555) 123-4567', '(', ''),
')', ''),
' ', ''),
'-', '') AS digits_only
FROM dual;The result is 5551234567.
The order matters when one search string can occur inside another replacement. Keep each transformation explicit, and test intermediate results when cleaning important data.
Common mistakes
- Expecting only the first match to change.
REPLACEchanges every non-overlapping match. - Forgetting case sensitivity.
'SQL'and'sql'are different search strings. - Assuming regular-expression behavior. Fixed strings and patterns require different functions.
- Omitting a needed nested call. Removing parentheses does not remove spaces or hyphens automatically.
- Ignoring
NULL. If the source isNULL, the result isNULL; a missing search match is different because it returns the source unchanged.
Exercise
What does this query return? SELECT REPLACE('A--B--C', '--', '-') FROM dual;
Show answer
It returns 'A-B-C'. Each non-overlapping occurrence of '--' is replaced by one hyphen.