Mysql Insert Function

MySQL: INSERT Function

This 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('AODBA.com', 1, 12, 'mysite');
Output: 'mysite.com'


mysql> SELECT INSERT('abcgh', 4, 2, 'def');
Output: 'abcdef'

mysql> SELECT INSERT('AODBA.com', 99, 12, 'mysite');
Output: 'AODBA.com'