MariaDB INSERT() Function

In MariaDB, INSERT() is a built-in string function that inserts a given string into another string at a specified position.

MariaDB INSERT() Syntax

Here is the syntax of the MariaDB INSERT() function:

INSERT(str, pos, len, newstr)

Parameters

str

Required. The string to process.

pos

Required. newstr The position at which to start inserting the new string . 1 Start with.

len

Required. The number of characters to be replaced.

newstr

Required. The new string to insert.

Return value

The MariaDB INSERT(str, pos, len, newstr) function returns the processed original string str, with the substring starting from pos and of length len is replaced by newstr.

If len is 0, the new string is inserted directly in pos without replacement.

If pos exceeds the string length, the INSERT() function returns the original string.

If len exceeds pos the remainder of the string starting at position, the INSERT() function replaces all characters starting pos.

If any argument is NULL, the INSERT() function will return NULL.

MariaDB INSERT() Examples

Basic Usage

The following statement shows the basic usage of the MariaDB INSERT() function:

SELECT
  INSERT('Hello World', 6, 1, '_'),
  INSERT('Hello World', 7, 5, 'Adam');

Output:

+----------------------------------+-------------------------------------+
| INSERT('Hello World', 6, 1, '_') | INSERT('Hello World', 7, 5, 'Adam') |
+----------------------------------+-------------------------------------+
| Hello_World                      | Hello Adam                          |
+----------------------------------+-------------------------------------+

NULL parameterss

The INSERT() function will return NULL if any argument is NULL.

SELECT
    INSERT(NULL, 6, 1, ' ') null_1,
    INSERT('Hello_World', NULL, 1, ' ') null_2,
    INSERT('Hello_World', 6, NULL, ' ') null_3,
    INSERT('Hello_World', 6, 1, NULL) null_4;

Output:

+--------+--------+--------+--------+
| null_1 | null_2 | null_3 | null_4 |
+--------+--------+--------+--------+
| NULL   | NULL   | NULL   | NULL   |
+--------+--------+--------+--------+

Conclusion

The MariaDB INSERT() function inserts another string at a specified position in a given string.