The OVERLAY function is used to replace a substring at a specified position and length within a string. Starting from the specified position, it replaces the specified length of characters with a new string.This function is multibyte safe.
This function behaves consistently with the INSERT function in MySQL.
OVERLAY(<str>, <pos>, <len>, <newstr>)
| Parameter | Description |
|---|---|
<str> | The original string to be modified. Type: VARCHAR |
<pos> | The starting position for replacement (1-based). Type: INT |
<len> | The length of characters to replace. Type: INT |
<newstr> | The new string to use for replacement. Type: VARCHAR |
Returns VARCHAR type, the new string after replacement.
Special cases:
<pos> is less than 1 or exceeds string length, no replacement occurs and returns the original string<len> is less than 0 or exceeds the remaining length, replaces from <pos> to the end of the stringSELECT overlay('Quadratic', 3, 4, 'What');
+------------------------------------+ | overlay('Quadratic', 3, 4, 'What') | +------------------------------------+ | QuWhattic | +------------------------------------+
SELECT overlay('Quadratic', 2, -1, 'Hi');
+-----------------------------------+ | overlay('Quadratic', 2, -1, 'Hi') | +-----------------------------------+ | QHi | +-----------------------------------+
SELECT overlay('Hello', 10, 2, 'X');
+-------------------------------+ | overlay('Hello', 10, 2, 'X') | +-------------------------------+ | Hello | +-------------------------------+
SELECT overlay('Hello', NULL, 2, 'X');
+--------------------------------+ | overlay('Hello', NULL, 2, 'X') | +--------------------------------+ | NULL | +--------------------------------+
SELECT INSERT('🎉🎊🎈', 2, 1, '🎁');
+--------------------------------------+ | INSERT('🎉🎊🎈', 2, 1, '🎁') | +--------------------------------------+ | 🎉🎁🎈 | +--------------------------------------+