MariaDB LTRIM, RTRIM, and TRIM: remove text at string edges

In MariaDB, LTRIM() removes spaces on the left, RTRIM() removes them on the right, and TRIM() removes them from both ends. The default target is an ordinary space. TRIM() also lets you choose the direction and the exact string to remove. Spacing inside the text stays in place.

Compare LTRIM, RTRIM, and TRIM directions

SELECT QUOTE(LTRIM('  12  34  ')) AS left_only,
       QUOTE(RTRIM('  12  34  ')) AS right_only,
       QUOTE(TRIM('  12  34  ')) AS both_ends;
left_only: '12  34  '
right_only: '  12  34'
both_ends: '12  34'

Run the first SELECT without a practice table. QUOTE() makes it easier to see where spaces remain. left_only keeps spaces after 34, right_only keeps spaces before 12, and both_ends strips both ends. All three keep the two spaces between 12 and 34. MariaDB Docs · MariaDB LTRIM() MariaDB Docs · MariaDB RTRIM()

Remove a specified string from the edges

In TRIM, LEADING means left, TRAILING means right, and BOTH means both ends. Omit the direction to use both ends, and omit the removal string to target ordinary spaces. Repeated stars are removed at the edges and the inner ** stays. The removal string ab matches in that order: ababXabab becomes X, and baXba stays as it is. MariaDB Docs · MariaDB TRIM()

SELECT TRIM('*' FROM '**12**34**') AS both_stars,
       TRIM(LEADING '*' FROM '**12**34**') AS leading_stars,
       TRIM(TRAILING '*' FROM '**12**34**') AS trailing_stars,
       TRIM(BOTH 'ab' FROM 'ababXabab') AS repeated_chunk,
       TRIM(BOTH 'ab' FROM 'baXba') AS different_order;

Check tabs, newlines, and nonbreaking spaces

SELECT HEX(TRIM(' abc ')) AS ordinary_spaces,
       HEX(TRIM(CONCAT(CHAR(9), ' abc ', CHAR(10)))) AS tab_and_lf,
       HEX(TRIM(CHAR(9) FROM CONCAT(CHAR(9), 'abc', CHAR(9)))) AS explicit_tab;

WITH sample AS (
    SELECT CONVERT(0xC2A0 USING utf8mb4) AS nbsp
)
SELECT HEX(TRIM(CONCAT(nbsp, 'abc', nbsp))) AS ordinary_trim,
       HEX(TRIM(BOTH nbsp FROM CONCAT(nbsp, 'abc', nbsp))) AS explicit_nbsp
FROM sample;

HEX() shows the bytes, including ones that are hard to see. The first result is 616263. The tab-and-newline example returns 0920616263200A. The tab and line feed are outside the ordinary spaces, so those spaces remain too. Explicitly trimming tabs from the tab-only edges gives 616263. MariaDB Docs · MariaDB HEX()

The nonbreaking space (U+00A0) in the second query is C2A0 in UTF-8. Default TRIM leaves C2A0616263C2A0. Explicitly targeting that character gives 616263. If tabs, line breaks and different spaces are mixed, inspect the source bytes with HEX() and agree on which characters should be removed.

Distinguish empty strings from NULL in a column

Use the learning practice database with CREATE, INSERT, and SELECT privileges. Choose a new practice database if the example table already exists. The first query shows the stored value, the trimmed display value, and a variant that converts an empty result to NULL.

USE learning;
CREATE TABLE trim_demo (
    id INT PRIMARY KEY,
    label VARCHAR(100) NULL
) DEFAULT CHARSET=utf8mb4;
INSERT INTO trim_demo VALUES
    (1, '  Pencil  '), (2, '    '), (3, NULL), (4, 'Memo  small');
SELECT id, QUOTE(label) AS original_value,
       QUOTE(TRIM(label)) AS trimmed_value,
       NULLIF(TRIM(label), '') AS empty_to_null
FROM trim_demo ORDER BY id;

In ordinary SQL mode, ID 1 loses its edge spaces, ID 2 becomes an empty string and ID 3 is NULL. ID 4 keeps the two interior spaces. NULLIF(TRIM(label), '') also displays the space-only ID 2 as NULL. Oracle compatibility mode and TRIM_ORACLE() can return NULL for an empty result. If behavior differs, check SELECT @@session.sql_mode and the function name. MariaDB Docs · MariaDB NULLIF() MariaDB Docs · MariaDB SQL_MODE

Preview the rows before changing stored values

SELECT id
FROM trim_demo
WHERE OCTET_LENGTH(label) <> OCTET_LENGTH(TRIM(label))
ORDER BY id;

The final query compares byte counts before and after TRIM with OCTET_LENGTH() and identifies IDs 1 and 2. This checks the physical length change even when the collation treats trailing spaces differently. Before storing changes, look for new duplicates and missing required values. If you use UPDATE, limit it with WHERE and prepare a backup and a transaction.