SQL Tutorial SQL Advanced SQL Database SQL References

Oracle REPLACE() Function



The Oracle (PL/SQL) REPLACE() function returns the given string with every occurrences of a specified substring replaced with a new substring. If the replacement_string is omitted or null, then all occurrences of search_string are removed. If search_string is null, then the given string is returned.

Syntax

REPLACE(string, search_string, replacement_string)

Parameters

string Required. Specify the source string.
search_string Required. Specify the substring to be replaced. All occurrences of search_string found within string will be replaced with replacement_string. If it is null, then string is returned.
replacement_string Optional. Specify the replacement substring. All occurrences of search_string found within string will be replaced with replacement_string. If it is omitted or null, then all occurrences of search_string are removed.

Return Value

Returns the string with the replaced values.

Example 1:

The example below shows the usage of REPLACE() function.

REPLACE('SQL Tutorial', 'SQL', 'HTML')
Result: 'HTML Tutorial'

REPLACE('xyz xyz', 'xyz', 'abc')
Result: 'abc abc'

REPLACE('100', '0', '5')
Result: '155'

REPLACE(100, 0, 5)
Result: '155'

REPLACE('xyz xyz', 'z')
Result: 'xy xy'

Example 2:

Consider a database table called Employee with the following records:

EmpIDNameCity
AXZ1JohnLondon
AXZ2MarryNew York
AXZ3JoParis
AXZ4KimAmsterdam
AXZ5RameshNew Delhi
AXZ6HuangBeijing

In the query below, the REPLACE() function is used to create a new column called NewEmpID by replacing 'AXZ' with 'XAY' from all records of EmpID column value.

SELECT Employee.*, 
REPLACE(EmpID, 'AXZ', 'XAY') AS NewEmpID 
FROM Employee;

This will produce the result as shown below:

EmpIDNameCityNewEmpID
AXZ1JohnLondonXAY1
AXZ2MarryNew YorkXAY2
AXZ3JoParisXAY3
AXZ4KimAmsterdamXAY4
AXZ5RameshNew DelhiXAY5
AXZ6HuangBeijingXAY6

❮ Oracle Functions