MariaDB INSERT() function: replace part of a string by position

MariaDB’s INSERT(str, pos, len, newstr) returns a string with len characters starting at pos replaced by newstr. Positions start at 1. A length of 0 puts the new text before the character at that position. INSERT INTO is a separate SQL statement for adding rows.

Replace, insert, or remove characters by position

str is the source string, pos is the starting position, len is the number of original characters to remove, and newstr is their replacement. The two lengths can differ. Run the first SELECT in a MariaDB client without creating a table. MariaDB Docs · MariaDB INSERT()

SELECT INSERT('1234567', 4, 2, 'ABC') AS replaced,
       INSERT('1234567', 4, 0, 'ABC') AS inserted,
       INSERT('1234567', 4, 2, '') AS removed;

123ABC67 replaces the fourth and fifth characters, 4 and 5, with ABC. 123ABC4567 inserts ABC before 4. 12367 removes that range by replacing it with an empty string. Applying the expression in SELECT leaves stored rows in place.

Check out-of-range positions and NULL

SELECT INSERT('abc', 0, 1, 'X') AS zero_position,
       INSERT('abc', -1, 1, 'X') AS negative_position,
       INSERT('abc', 4, 0, 'X') AS after_end,
       INSERT('abc', 3, 0, 'X') AS before_last,
       INSERT('abcdef', 3, 99, 'X') AS past_remaining,
       INSERT('abc', 2, 1, NULL) AS null_replacement;

In abc, positions 0, -1, and 4 are outside the valid range, so those expressions return abc. Inserting with length 0 at position 3 gives abXc, with the new value before the last character. To append X, run the CONCAT() example below to get abcX. MariaDB Docs · MariaDB CONCAT()

SELECT CONCAT('abc', 'X') AS appended;

In abcdef, a length of 99 starting at position 3 replaces the entire remaining substring cdef, giving abX. Any NULL argument yields NULL. Validate a calculated position against 1 through CHAR_LENGTH(str) and use a nonnegative count of characters to remove.

Count positions in multibyte text

SELECT CHAR_LENGTH(_utf8mb4'가나다라') AS characters,
       OCTET_LENGTH(_utf8mb4'가나다라') AS bytes,
       INSERT(_utf8mb4'가나다라', 2, 2, 'X') AS replaced_text;

The sample 가나다라 contains four utf8mb4 characters and 12 bytes. Replacing two characters from position 2 returns 가X라. Compare CHAR_LENGTH() for characters and OCTET_LENGTH() for bytes. A binary value can use byte positions. A displayed grapheme, such as a character with combining marks or a combined emoji, can contain several encoded characters. MariaDB Docs · MariaDB CHAR_LENGTH() MariaDB Docs · MariaDB OCTET_LENGTH()

Validate an order code before masking part of it

The next example uses the learning practice database and requires CREATE, INSERT, and SELECT privileges. Use a fresh practice database if the table already exists. The order codes are synthetic. The query masks six characters starting at position 10 only when the code is exactly 15 characters, with an uppercase ORD- prefix and the required groups of digits. MariaDB Docs · MariaDB REGEXP

USE learning;
CREATE TABLE string_insert_demo (
    id INT PRIMARY KEY,
    order_code VARCHAR(40) NULL
) DEFAULT CHARSET=utf8mb4;
INSERT INTO string_insert_demo VALUES
    (1, 'ORD-2026-000123'), (2, 'SHORT'), (3, NULL),
    (4, 'ord-2026-000123'),
    (5, CONCAT('ORD-2026-000123', CHAR(10)));

SELECT id,
       CASE
           WHEN CHAR_LENGTH(order_code) = 15
                AND BINARY order_code REGEXP '^ORD-[0-9]{4}-[0-9]{6}$'
           THEN INSERT(order_code, 10, 6, '******')
           ELSE '[Check format]'
       END AS display_code
FROM string_insert_demo ORDER BY id;

ID 1 displays ORD-2026-****** and IDs 2 to 5 display a validation message. Those cases cover a short code, NULL, a lowercase prefix, and a final line feed. BINARY makes the prefix check case sensitive, and the length check prevents a trailing newline from passing. Check independently whether other responses or logs still expose the stored code.

Choose between INSERT() and REPLACE()

SELECT INSERT('A-B-A', 1, 1, 'X') AS positional,
       REPLACE('A-B-A', 'A', 'X') AS by_value;

Use INSERT() when the start and length are already known. Use REPLACE() to find specific text and replace every occurrence. With A-B-A, these give X-B-A and X-B-X, respectively. MariaDB Docs · MariaDB REPLACE()

Before changing stored values, use SELECT to inspect the target and the converted output. Prepare the UPDATE with a WHERE condition, a backup, and a transaction before applying the change.