INSERT() function in MySQL

INSERT() :
This function in MySQL is used for inserting a string within a string, removing a number of characters from the original string.

Syntax :

INSERT(str, pos, len, newstr)

Parameters :
This method accepts four parameter.

  • str – Original string in which we want to insert another string.
  • pos – The position where we want to insert another string.
  • len – The number of characters to replace.
  • newstr – The string to be inserted.

Returns :
It returns a newly formed string.
Example-1 :
Inserting the string “mysql” into the string “w3wiki” and replacing five characters, starting from position 9 with the help of INSERT Function.

SELECT INSERT("w3wiki", 9, 5, "MySQL") 
AS NewString ;

Output :

NEWSTRING
Beginnerformysql

Example-2 :
The following MySQL statement returns Original string, the actual string itself. This happens because the position of insertion, which is specified as -5, is out of range, so no insertion takes place.

SELECT INSERT("w3wiki", -5, 5, "MySQL") 
AS NewString ;

Output :

NEWSTRING 
w3wiki

Example-3 :
The following MySQL statement returns a completely new string. This happens because the position of insertion is 1 and length is number of character in previous string.

SELECT INSERT("w3wiki", 1, 13, "stackoverflow") 
AS NewString ;

Output :

NEWSTRING
stackoverflow

Contact Us