Open In App

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.

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



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

Output :

NEWSTRING
geeksformysql

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("geeksforgeeks", -5, 5, "MySQL") 
AS NewString ;

Output :

NEWSTRING 
geeksforgeeks

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("geeksforgeeks", 1, 13, "stackoverflow") 
AS NewString ;

Output :

NEWSTRING
stackoverflow
Article Tags :
SQL