MSSQL STUFF Function and Its Equivalent in MySQL


The STUFF function embeds a string within another string by replacing a specified number of characters. The same functionality is provided by the INSERT function in MySQL.

MSSQL

Syntax:

STUFF ( character_expression , start , length , replaceWith_expression )

Example — embed '1234' at the second position, replacing the characters 'bcd' in the string 'abcdefgh':

select stuff('abcdefgh',2,3,'1234')
-----------------------
a1234efgh

MSSQL STUFF function example

MySQL

In MySQL, the same functionality is provided by INSERT.

Syntax:

INSERT(str, pos, len, newstr)

Returns the string str, with the substring beginning at position pos and len characters long replaced by newstr. Returns the original string if pos isn't within the string's length; replaces the rest of the string from pos if len isn't within the remaining length; returns NULL if any argument is NULL.

Example:

mysql> select INSERT('12345678',2,3,'abc');
+--------------------------------+
| INSERT('12345678',2,3,'abc')   |
+--------------------------------+
| 1abc5678                       |
+--------------------------------+

mysql> select INSERT('abcdefgh',2,3,'1234');
+---------------------------------+
| INSERT('abcdefgh',2,3,'1234')   |
+---------------------------------+
| a1234efgh                       |
+---------------------------------+

INSERT function in MySQL Example

Back to Converting Functions from MSSQL to MySQL