MySQL: INSERT Function
This MySQL tutorial explains how to use the MySQL INSERT function with syntax and examples.
Description
The MySQL INSERT function inserts a substring into a string at a specified position for a certain number of characters.
Syntax
The syntax for the INSERT function in MySQL is:
INSERT( string, position, number, substring )
Parameters or Arguments
- string
- The string to modify.
- position
- The position in string to insert substring.
- number
- The number of characters to replace in string.
- substring
- The substring to insert into string.
Note
- The first position in string is 1.
- If position is not within the length of string, the INSERT function will return string.
- If number is not within the length of the rest of the string, the INSERT function will replace string starting from position until the end of string.
Applies To
The INSERT function can be used in the following versions of MySQL:
- MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0, MySQL 3.23
Example
Let's look at some MySQL INSERT function examples and explore how to use the INSERT function in MySQL.
For example:
mysql> SELECT INSERT('techonthenet.com', 1, 12, 'checkyourmath'); Result: 'checkyourmath.com' mysql> SELECT INSERT('Tech on the Net', 13, 3, 'Internet'); Result: 'Tech on the Internet' mysql> SELECT INSERT('abcgh', 4, 2, 'def'); Result: 'abcdef' mysql> SELECT INSERT('techonthenet.com', 99, 12, 'checkyourmath'); Result: 'techonthenet.com'
Advertisements