Introduction: Oracle REPLACE Function — Complete Guide

The REPLACE function in Oracle is a simple yet powerful string manipulation tool used to substitute occurrences of a substring within a string.

Syntax

The basic syntax is quite simple:

REPLACE(source_string, search_string [, replacement_string])
• source_string -- Original text
• search_string -- Text to find
• replacement_string -- Text to replace with (optional)

If the replacement_string is omitted, Oracle removes the search_string from the source string.

Basic Examples

Simple Replacement

SELECT REPLACE('Hello World', 'World', 'Oracle') AS result FROM dual;

Output:

Hello Oracle

Lab output example:

Screenshot of an SQL query builder displaying two SELECT statements that replace 'World' with 'Oracle' and 'ACEs!' in the string 'Hello World', showing the resulting outputs.
Formatting Output

in this case, the systems setup will display the date information by using the . as separator:

SQL query output showing the current system date as 01.05.2026

But I would like to have ‘/ ‘ as separator! This can also be easily achieved by using the REPLACE function:

SELECT REPLACE(sysdate, '.', '/') AS formatted_date
FROM dual;
FORMATTED_
----------
01/05/2026
SQL query output showing a formatted date from the sysdate function, with the result displayed as '01/05/2026'.

Removing Text

SELECT REPLACE('Hello World', 'World') AS result FROM dual;

Output:

Hello

Or:

SELECT REPLACE('Removing Text', 'Text') AS result
FROM dual;

Output:

Removing

Screenshot of an SQL query result displaying the output of a SQL command that replaces 'Text' with an empty string in 'Removing Text', resulting in 'Removing'.

Case Sensitivity

Oracle REPLACE is case-sensitive.

SELECT REPLACE('Hello World', 'world', 'Oracle') AS result
FROM dual;
Output:
Hello World

Or, lab example:

SQL query demonstrating the REPLACE function with results displayed. The first query replaces 'world' in 'Hello World', and the second replaces 'text' in 'Removing Text'.

Practical Use Cases I have encountered in real-life Clients scenarios

Data Cleaning / scrubbing: (Remove Special Characters)

Suppose we loaded in a table a flat file presented as an external table but the file data is now corrupted by ‘-‘ characters! Our task is to remove the ‘-‘ characters from the telephone number fields.

Use case: Normalize phone numbers

like so:

123-456-7890 → 1234567890

The basic function call that would help us update the table is:

SELECT REPLACE(phone_number, '-', '') AS cleaned_phone
FROM customers;

In the lab example we start with a table containing corrupt phone numbers:

SQL query result displaying two customer entries with their corresponding phone numbers.

next step is to update the table data:

Screenshot of a SQL query result showing the command to update customer phone numbers by replacing specific characters, with a confirmation that 2 rows were updated.

Commit and check the results:

Screenshot of a database query output showing two updated rows with the names 'Customer 1' and 'New Customer' alongside their phone numbers in a tabular format.

Now the data in the phone number column is cleaned of the undesired characters.

Standardizing Text Values

In another situation, we received and uploaded without checking a set of files with a lot of country names that would not be according to desired standard. In our case, for example: ‘U.S .A’ and not ‘USA’! As a consequence, the wrong data would cause application unhandled exception crashes during validation! The fix on database side, is extremely easy by using the REPLACE function:

the table would look like so in the lab environment:

SQL query results showing a list of customers with their phone numbers and country, indicating two entries: 'Customer 1' and 'New Customer', both from the U.S.

Checking how the REPLACE function would work:

SELECT REPLACE(country, 'U.S.A', 'USA') AS standardized_country
FROM customers;

We can see that we would get the desired result according to standards:

Screenshot of a SQL query output showing the result of a 'SELECT REPLACE' command, displaying 'USA' under the header 'STANDARDIZED_COUNTRY'.

After running an update and checking again, the problem was easily solved:

update customers set country = REPLACE(country, 'U.S.', 'USA');
SQL command showing an update of customer country from 'U.S.' to 'USA', with confirmation of 2 rows updated and task completion time.

the result is now as expected!

Screenshot of a database query result showing updated customer information, including names, phone numbers, and countries.
Masking Sensitive Data

Another use case is masking sensitive data by using the replace function when ,for example when the code publishes some information in the application interface.

Here is an example:

Right now, I have the credit card information in my customers table:

select * from Customers;
COLUMN1 PHONE_NUMBER COUNTRY CRDITCARD#
-------------------- -------------- ---------- --------------
Customer 1 1234567890 USA 58392017465028
New Customer 1234567890 USA 94173620581947

But I would like to mask their first 12 digits, I cna achieve that by using a combination of REPLACE function with SUBSTR:

select REPLACE(crditcard#, SUBSTR(crditcard#, 1, 12), '************') AS masked_cc
FROM customers
;
MASKED_CC
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
************28
************47
SQL query displaying masked credit card numbers in a database result.
Removing HTML Tags
Simple Case

Sometimes, we get upload files corrupted by an external application with a lot of HTML tags. In this case we have uploaded the file content in the description field. With a simple approach, the use of REPLACE function can help like so:

SELECT REPLACE(description, '<br>', '') AS clean_text
FROM products;
Complex HTML removal

The use REGEXP_REPLACE, can be more powerful in many cases, but I leave this topic to be covered in a future article.

Bulk Updates in Tables

The use of REPLACE function is very beneficial from performance point of view and easy to be implemented in the code.

Here is another example of changing the Old Company information to the New Company, for example after an acquisition:

The current table has the old email domain information:

Table displaying customer information including names, phone numbers, country, credit card numbers, and email domains.

But, after the migration the new domain should be used!

This can be updated in the database, by using REPLACE function:

UPDATE customers
SET email_domain = REPLACE(email_domain, 'oldcompany.com', 'newcompany.com');

Afterwards, the table has all the new company data:

COLUMN1 PHONE_NUMBER COUNTRY CRDITCARD# EMAIL_DOMAIN
-------------------- -------------- ---------- -------------- ---------------
Customer 1 1234567890 USA 58392017465028 newcompany.com
New Customer 1234567890 USA 94173620581947 newcompany.com
Table displaying customer information including names, phone numbers, countries, credit card numbers, and email domains.

Using REPLACE in PL/SQL

is quite straightforward and performing well! We cal also use the variables defined in the code.

Here is an example:

set serveroutput on
DECLARE
v_text VARCHAR2(100) := 'Oracle is powerful';
BEGIN
DBMS_OUTPUT.PUT_LINE(v_text);
v_text := REPLACE(v_text, 'powerful', 'amazing');
DBMS_OUTPUT.PUT_LINE(v_text);
END;
/
PL/SQL code snippet demonstrating the usage of DBMS_OUTPUT to display modified string variables. The initial string 'Oracle is powerful' is replaced to 'Oracle is amazing' and both outputs are shown in the script output area.
Nested REPLACE (Multiple Substitutions)

The functions can be nested as the result from call can be an argument for another call!

In this case we will remove the ‘-‘ character from the string and REPLACE A with X:

SELECT REPLACE(REPLACE('A-B-C', '-', ''), 'A', 'X') AS result
FROM dual;
SQL query showing a nested REPLACE function, replacing characters in a string with the result displayed as 'XBC'.

REPLACE vs REGEXP_REPLACE

Here are a few subjective conclusions I have reached over time from using those two functions:

FeatureREPLACEREGEXP_REPLACE
Simple text substitution
Pattern matching
PerformanceFastSlower
ComplexityLowHigh
Example Comparison

Provided to highlight the case of use of the REPLACE function. we can also note the execution of this simple case is almost the same.

Using REPLACE:
SELECT REPLACE('abc123', '123', '') FROM dual;
SQL query result showing the use of the REPLACE function to remove '123' from 'abc123', resulting in 'abc'.
Using REGEXP_REPLACE:
SELECT REGEXP_REPLACE('abc123', '[0-9]', '') FROM dual;
SQL query using REGEXP_REPLACE function to remove digits from the string 'abc123', resulting in 'abc'.
Performance Tip

Use REPLACE when:

  • Exact match is known
  • No pattern needed

Avoid chaining too many nested REPLACE calls → may reduce readability

Common Pitfalls
Treat Case Sensitivity with care!

Use UPPER when / if needed:

SQL query example showing the REPLACE and UPPER functions, with output 'HELLO Oracle'.

Note the even now, the letter cases of the word ‘Hello’ were changed in this example according to the UPPER function!

NULL Behavior

Be mindful of the NULL values and the results returned after a function call!

SQL query output showing the result of a SELECT statement with REPLACE function returning NULL.
Empty Replacement

Always be mindful of the empty replacement cases and test them!

When NOT to Use only REPLACE

Avoid REPLACE function only when:

  • You need pattern-based replacement → use REGEXP_REPLACE
  • You need position-based edits → combine with the use of SUBSTR as shown above

Summary

  • REPLACE is fast and simple for direct substitutions
  • Works in both SQL and PL/SQL
  • Best for data cleaning, formatting, and normalization
  • Case-sensitive and does not support patterns (use REGEXP_REPLACE)

Discover more from Radu Pârvu

Subscribe to get the latest posts sent to your email.


Leave a Reply

Discover more from Radu Pârvu

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from Radu Pârvu

Subscribe now to keep reading and get access to the full archive.

Continue reading