REPLACE
Overview
The REPLACE()
function looks for and replaces a substring with a new one in a string. This function is often used to update the outdated or spelling mistakes in data that require an amendment.
REGEXP_REPLACE()
function. It enables you to search and replace a substring(s) that matches with a POSIX regular expression. See here for more info.Syntax
The REPLACE()
function syntax is described as follows.
The syntax requires three arguments, explained below:
-
string
: the string that you want to replace. -
old_substring
: the substring that you want to replace. All parts will be replaced if it appears multiple times in the string. -
new_substring
: the new substring that will replace the old one**.**
REPLACE()
function performs a case-sensitive replacement.Examples
Case #1: Basic REPLACE()
function
We will replace the substring of the “OxlaDatabase” string from “**New” **to “Oxla”, as shown below:
The REPLACE()
function will find and replace all occurrences in the string and return the following:
Case #2: Replacing the specified values in a table
This example shows how to replace the values of a specific column in a table.
- Create a new table named extracurriculars with club and category columns and insert the values into the respective columns.
- Retrieve all values from the table using the following query.
- Replace the “group” value in the category column with** “sports”.**
- The final result will look like the following.
Case #3: Remove a word with REPLACE()
function
In the following example, we will remove **“Friends” **in a string with a REPLACE()
function.
The final output will leave the remaining word, “Hello”.
Case #4: Replace multiple patterns with REPLACE()
function
The following example uses the REPLACE()
function to replace multiple patterns of the given string.
We can see that the REPLACE function is called multiple times to replace the corresponding string as specified:
-
the
[]
into()
-
the
{}
into()