Skip to content
Related Articles

Related Articles

INSERT() function in MySQL
  • Last Updated : 09 Dec, 2020

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 “geeksforgeeks” and replacing five characters, starting from position 9 with the help of INSERT Function.

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

Output :


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 :


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 :


Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :