search
Search
Login
Math ML Join our weekly DS/ML newsletter
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
brightness_medium
share
arrow_backShare
Twitter
Facebook

MySQL | INSERT method

Database
chevron_right
MySQL
chevron_right
Documentation
chevron_right
Functions
chevron_right
String
schedule Jul 1, 2022
Last updated
local_offer MySQL
Tags

MySQL's INSERT(~) method inserts and replaces the specified number of characters len from a substring newstr at the specified position pos in the destination string str.

Parameters

1. str | string

The string where we will insert the substring.

2. pos | index

The starting position in str where the substring should be inserted.

3. len | number

The number of characters from the substring to insert and replace in the original string.

4. newstr | string

The substring to insert.

Return value

The string str, with the substring beginning at position pos and len characters long replaced by the string newstr.

Examples

Basic usage

To insert 2 characters from the substring 'Ph' at the start of 'Filming':

SELECT INSERT('Filming', 1, 2, 'Ph');
+----------------------------+
| INSERT('Filming',1,2,'Ph') |
+----------------------------+
| Phlming |
+----------------------------+

The 'Fi' in the original string is replaced by the new string 'Ph' which gives us return value 'Phlming'.

Pos parameter

The original string is returned if the provided pos is not within the length of the string:

SELECT INSERT('Filming', 9, 2, 'Ph');
+-------------------------------+
| INSERT('Filming', 9, 2, 'Ph') |
+-------------------------------+
| Filming |
+-------------------------------+

The start position of 9 is not within the length of 'Filming' hence the original string is returned.

Len parameter

The rest of the string is replaced if the provided len is not within the length of the string:

SELECT INSERT('Falafel', 5, 4, 'phil');
+---------------------------------+
| INSERT('Falafel', 5, 4, 'phil') |
+---------------------------------+
| Falaphil |
+---------------------------------+

We insert 4 characters 'phil' starting from position 5 in 'Falafel', despite the fact there are only three characters to replace ('fel') if we start from position 5.

Null argument

NULL is returned if any argument is NULL:

SELECT INSERT('Falafel', 5, 4, NULL);
+-------------------------------+
| INSERT('Falafel', 5, 4, NULL) |
+-------------------------------+
| NULL |
+-------------------------------+
mail
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
robocat
Published by Arthur Yanagisawa
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!