SQL REPLACE Function Tutorial

In SQL, the REPLACE function is used to replace values in a specific field. The syntax of the REPLACE function is as follows:

REPLACE(string, old_value, new_value)

Explanation of parameters:

  1. String: the string or field that requires replacement operation.
  2. old_value: the value that needs to be replaced.
  3. new_value: the new value used for replacement.

For example, suppose there is a table called “customers” with a field named “address”. If we need to replace all values containing “Street” in the “address” field with “Road”, the following SQL statement can be used:

UPDATE customers
SET address = REPLACE(address, 'Street', 'Road')

After executing the SQL statement above, all values in the “address” field containing “Street” will be replaced with “Road”.

bannerAds