INSERT() FUNCTION in MySQL

INSERT() FUNCTION
The MySQL INSERT function is used to insert a substring into a string. This insertion is done at a specified position and for a certain number of characters. The various versions of MySQL support the INSERT function, namely, MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0 and MySQL 3.23.

Syntax:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
INSERT( string, position, number, substring )
INSERT( string, position, number, substring )
INSERT( string, position, number, substring )

Parameters:
string: It is used to specify the string to modify.
position: It is used to specify the position for insertion.
number: It is used to specify the number of characters to replace during insertion.
substring: It is used to specify the substring to insert.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mysql> SELECT INSERT ( 'HELLO WORLD', 7, 5, ‘FRIENDS’);
mysql> SELECT INSERT ( 'HELLO WORLD', 7, 5, ‘FRIENDS’);
mysql> SELECT INSERT ( 'HELLO WORLD', 7, 5, ‘FRIENDS’);

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
HELLO FRIENDS
‘HELLO FRIENDS’
‘HELLO FRIENDS’

Explanation:
The substring is inserted at the 7th position and replaces the 5 characters in the specified string.