The INSERT() function in SQL is a string function which is used to replace a specified number of characters in a string with another string. It takes the starting position as well as the number of characters to replace as arguments and returns the new modified string.
The INSERT() string function is mainly utilized in MySQL to modify or replace a part of an existing string without changing the original value stored in the database.
The INSERT() in SQL is a string function by which one string is inserted into another string at a specified position. It replaces some specific number of characters starting from some position with a new set of characters.
The syntax of the INSERT() string function is as follows:
INSERT(string, position, length, new_string)
The function takes four parameters which are as follows:
string: It is the original string in which the alteration will be done.
position: It is the starting position from which the characters are replaced. The first character has position 1.
length: It specifies the number of characters to replace.
new_string: It is the string that replaces the specified characters.
We will take the help of the sample table below named "customers" in the examples to illustrate the working of the INSERT() string function.
Table: customers
| CustomerID | CustomerName | Phone | |
|---|---|---|---|
| 1 | Rahul Sharma | 9876543210 | rahulsharma@gmail.com |
| 2 | Priya Singh | 8765432109 | priyasingh@gmail.com |
| 3 | Amit Kumar | 7654321098 | amitkumar@gmail.com |
| 4 | Neha Verma | 6543210987 | nehaverma@gmail.com |
| 5 | Seema Patel | 6432109876 | seemapatel@gmail.com |
The INSERT() function can be utilized to replace a specific number of characters in a string. In this example, we replace the word 'World' with SQL.
SQL Query:
SELECT INSERT('Hello World', 7, 5, 'SQL') AS Result;Output:
Hello SQL
Explanation:
In the above example, 7 is the starting position, 5 signifies the number of characters to replace and 'SQL' is the new string.
The INSERT() function can also be used to insert text into a string without removing any existing characters. To do this, we can specify 0 as the number of characters to replace.
SQL Query:
SELECT INSERT('Hello World', 6, 0, ' SQL') AS Result;Output:
Hello SQL World
Explanation:
The function starts at position 6 and replaces 0 characters. Therefore 'SQL' is inserted at that position without removing any existing characters.
We can also utilize the INSERT() function with table columns. Suppose we want to replace the first three digits of each customer's phone number with XXX.
SQL Query:
SELECT CustomerName, INSERT(Phone, 1, 3, 'XXX') AS MaskedPhone FROM Customers;
Output:
| CustomerName | MaskedPhone |
|---|---|
| Rahul Sharma | XXX6543210 |
| Priya Singh | XXX5432109 |
| Amit Kumar | XXX4321098 |
| Neha Verma | XXX3210987 |
| Seema Patel | XXX2109876 |
Explanation:
In the above example, 1 is the starting position and 3 specifies the number of characters that need to be replaced in the Phone column with XXX. It masks the first three digits of each phone number.
We can use the function with an Email column to replace part of an email address. Here we will replace the first five characters with asterisks.
SQL Query:
SELECT CustomerName, INSERT(Email, 1, 5, '*****') AS MaskedEmail FROM Customers;
Output:
| CustomerName | MaskedEmail |
|---|---|
| Rahul Sharma | *****sharma@gmail.com |
| Priya Singh | *****singh@gmail.com |
| Amit Kumar | *****umar@gmail.com |
| Neha Verma | *****verma@gmail.com |
| Seema Patel | *****patel@gmail.com |
Explanation:
The function starts at position 1 and replaces the first 5 characters of the Email column with *****. The remaining characters remain unchanged.
We can utilize this function to replace characters from any position in a string. In this example, we will replace the word 'Tutorial' with 'Function'.
SQL Query:
SELECT INSERT('SQL Tutorial', 5, 8, 'Function') AS Result;Output:
SQL Function
Explanation:
Here the function starts at position 5 and replaces 8 characters. The eight characters in 'Tutorial' are replaced with 'Function'.
The INSERT() function returns NULL when the original string is NULL.
SQL Query:
SELECT INSERT(NULL, 1, 3, 'SQL') AS Result;
Output:
NULL
Explanation:
Since the original string is NULL then the INSERT() function returns NULL. Similarly, if the new string is NULL then the function also returns NULL.
Both INSERT() and CONCAT() are string functions but they perform different operations which we will see in the difference table below:
| INSERT() | CONCAT() |
|---|---|
| It replaces a specified part of an existing string. | It combines two or more strings. |
| It requires a starting position and length. | It does not require a position or length. |
| It is used to modify a specific part of a string. | It is used to join strings together. |
| Example: INSERT('Hello World', 7, 5, 'SQL') | Example: CONCAT('Hello', ' ', 'World') |
| Output: Hello SQL | Output: Hello World |
The INSERT() and REPLACE() functions can both be used to modify strings but they work differently. Below is a difference table:
| INSERT() | REPLACE() |
|---|---|
| It replaces characters based on their position. | It replaces a specified substring wherever it occurs. |
| It requires a starting position and length. | It requires the string to search for and its replacement. |
| Syntax: INSERT(string, position, length, new_string) | Syntax: REPLACE(string, search_string, replacement_string) |
| It is useful when the exact position of the characters is known. | It is useful when the specific substring is known. |
We request you to subscribe our newsletter for upcoming updates.