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:
EmpID | Name | City |
---|---|---|
AXZ1 | John | London |
AXZ2 | Marry | New York |
AXZ3 | Jo | Paris |
AXZ4 | Kim | Amsterdam |
AXZ5 | Ramesh | New Delhi |
AXZ6 | Huang | Beijing |
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:
EmpID | Name | City | NewEmpID |
---|---|---|---|
AXZ1 | John | London | XAY1 |
AXZ2 | Marry | New York | XAY2 |
AXZ3 | Jo | Paris | XAY3 |
AXZ4 | Kim | Amsterdam | XAY4 |
AXZ5 | Ramesh | New Delhi | XAY5 |
AXZ6 | Huang | Beijing | XAY6 |
❮ Oracle Functions