Open In App

INSERT() function in MySQL

Last Updated : 09 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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 “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

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads