INSERT() - Insert and Replace Substring

Q

How to replace a substring at a given position with a given string using the INSERT() function?

✍: FYIcenter.com

A

INSERT(str, pos, len, newstr) is a MySQL built-in function that locates a substring in a string and replace it with a new string. For example:

SET @str = 'dba.FYIcenter.com';

SELECT INSERT(@str, 1, 3, 'dev');
  -- +---------------------------+
  -- | INSERT(@str, 1, 3, 'dev') |
  -- +---------------------------+
  -- | dev.FYIcenter.com         |
  -- +---------------------------+

Reference information of the INSERT() function:

INSERT(str, pos, len, newstr): modstr
  Returns the string str, with the substring beginning at position pos and
  len characters long replaced by the string newstr. Returns the original
  string if pos is not within the length of the string. Replaces the rest
  of the string from position pos if len is not within the length of the
  rest of the string. Returns NULL if any argument is NULL.

Arguments, return value and availability:
  str: Required. The string to be modified.
  pos: Required. The starting position of the substring to be replaced.
  len: Required. The number of characters of the substring.
  newstr: Required. The new string to replace the substring.
  modstr: Return value. The modified string.
  Available since MySQL 4.0.

 

INSTR() - Find Substring

HEX() - Calculate HEX representation

MySQL Functions on Character String Values

⇑⇑ MySQL Function References

2023-11-12, 1198🔥, 0💬