Update MySQL Records Using Replace Function

Update DATABASE records directly using the REPLACE() function in MySQL. No scripting or programming language needed. Can be executed in any MySQL visual database design tool, or make the modification directly from shell.

Requirements:

  • MySQL

Consider below as the employees TABLE inside a MySQL DATABASE.

The objective is to update MySQL data records by replacing all spaces with a dash from all the employees first names. The update must be permanently stored into the first_name COLUMN.

UPDATE
    employees
SET
    first_name=REPLACE(
        first_name, ' ', '-'
    )
;

Where.

  • first_name is the COLUMN name representing first_name records.
  • ‘ ‘ is the search string.
  • ‘-‘ is the replacement string.

Result.

Before.

After.

Notes:

  • The REPLACE() function performs a case-sensitive match when searching for search string.
  • It will return NULL if any of its arguments are NULL.

References:


Posted

in

by

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *