MySQL INSERT() Function
In this context, we will learn how we can use the MySQL INSERT() function with proper syntax and good examples.
Introduction of MySQL INSERT() function
Basically, in MySQL, the INSERT function is used for adding a string within a string and removing a number of characters from the main string.
Syntax of the MySQL INSERT() function
The syntax of the MySQL INSERT() function is given as follows:
INSERT(str, pos, len, newstr)
Parameters or arguments used in MySQL INSERT() function:
There are four parameters accepted by the INSERT() function in MySQL, which are mentioned below:
Str- It is the main string in which we want to add another string.
Pos – It is the position where we want to add another string.
Len – The number of characters to replace.
newstr – The string to be inserted.
Returns:
It will return a newly formed string.
Application used for INSERT() function:
The INSERT() function can be used in the given below MySQL versions.:
- MySQL 5.7
- MySQL 5.6
- MySQL 5.5
- MySQL 5.1
- MySQL 5.0
- MySQL 4.1
- MySQL 4.0
- MySQL 3.23
Examples of MySQL INSERT() function:
Now we will look into some MySQL INSERT() function examples and will explore how we can use the INSERT function in MySQL.
Example-1 :
Here, we will insert the string "MySQL" into the string "javatpoint" and replace five characters, starting from position 6 with the help of the INSERT Function.
SELECT INSERT("javatpoint," 6, 5, "MySQL")
AS NewString ;
Output:
NEWSTRING
javatmysql
1 row in set (0.00 sec)
Example-2 :
The following MySQL statement returns the 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("javatpoint," -5, 5, "MySQL")
SELECT INSERT("javatpoint," -5, 5, "MySQL")
AS NewString ;
Output:
NEWSTRING
geeksforgeeks
1 row in set (0.00 sec)
Example-3 :
The following MySQL statement returns a completely new string. This happens because the insertion position is 1, and length is the number of characters in the previous string.
SELECT INSERT("javatpoint," 1, 13, "StackOverflow")
AS NewString ;
Output:
NEWSTRING
Stackoverflow
1 row in set (0.00 sec)
Application of MySQL INSERT() function:
This function is used for adding a string within a string and removing a number of characters from the main string.
Summary:
In the above context, we have learned how we can use the INSERT() function in MySQL used for adding a string within a string and removing a number of characters from the main string.