How is the SQL REPLACE function used?
The SQL REPLACE function is used to replace one substring in a string with another substring. Its syntax is as follows:
Return a string where a specified substring is replaced with a new substring.
Description of parameters:
- str:要进行替换的字符串。
- substring_to_be_replaced: the substring that will be replaced.
- new_substring: The substring used for replacement.
Suppose there is a table called “employees” with a column “first_name” storing employees’ names. If we want to replace all occurrences of “John” with “Tom”, we can use the following SQL statement:
Update the employees table by replacing ‘John’ with ‘Tom’ in the first name column.
This will replace “John” with “Tom” in all names.