Friday, December 20, 2024

MySQL REPLACE() Function

 The REPLACE() function in MySQL is used to replace all occurrences of a substring within a string with a new substring. This function returns a string with the replacements made.

Syntax:

REPLACE(string, old_substring, new_substring)
  • string: The original string where the replacement will occur.
  • old_substring: The substring that you want to replace.
  • new_substring: The substring that will replace the old substring.

Example:

Let's say you have a string 'Hello World' and you want to replace 'World' with 'MySQL':

SELECT REPLACE('Hello World', 'World', 'MySQL');

This would return:

Hello MySQL

Notes:

  • If the old_substring is not found in the string, the original string is returned unchanged.
  • The REPLACE() function is case-sensitive.
  • It replaces all occurrences of the old_substring in the string.

No comments:

Post a Comment