MariaDB LEFT() and RIGHT(): Extract the First or Last Characters

In MariaDB, LEFT(string, n) returns the first n characters and RIGHT(string, n) returns the last n. The characters keep their original order, and both functions leave the stored value untouched.

Compare the two directions

The second argument of LEFT() and RIGHT() gives the number of characters to return. To try both directions, connect to MariaDB and run these two statements. They require no table.

SELECT LEFT('12345678', 3) AS first_three,
       RIGHT('12345678', 3) AS last_three;
SELECT LEFT('AB-2026-0042', 2) AS code_prefix,
       RIGHT('AB-2026-0042', 4) AS code_suffix;

The first row returns 123 and 678. The second returns AB and 0042. Keep the suffix as text when leading zeros matter.

Zero, negative, and missing lengths

Brackets make an empty string visible in the output. A zero or negative length produces an empty string. If the requested length exceeds the input length, MariaDB returns the whole string without padding.

SELECT CONCAT('[', LEFT('abc', 0), ']') AS zero_left,
       CONCAT('[', RIGHT('abc', -1), ']') AS negative_right,
       LEFT('abc', 20) AS long_left,
       RIGHT('abc', 20) AS long_right;
SELECT LEFT(NULL, 2) AS missing_text,
       RIGHT('abc', NULL) AS missing_length,
       CONCAT('[', RIGHT('', 2), ']') AS empty_text;

Expect [], [], abc, abc in the first row and NULL, NULL, [] in the second. Either argument can cause a NULL result. Validate the integer range when the length comes from user input, and handle missing values explicitly when displaying them as blanks.

Unicode text, bytes, and spaces

Save the script in UTF-8 or paste it into a UTF-8 client. Use a separate practice connection because SET NAMES utf8mb4 applies to that connection. Use CHAR_LENGTH() for character counts and OCTET_LENGTH() for byte counts.

SET NAMES utf8mb4;
SELECT LEFT('가나다라마', 2) AS left_hangul,
       RIGHT('가나다라마', 2) AS right_hangul,
       CHAR_LENGTH(LEFT('가나다라마', 2)) AS character_count,
       OCTET_LENGTH(LEFT('가나다라마', 2)) AS byte_count;
SELECT HEX(LEFT(' AB ', 2)) AS left_with_space,
       HEX(RIGHT(' AB ', 2)) AS right_with_space;

The first row returns 가나 and 라마. The prefix has two characters and takes six UTF-8 bytes. In the second row, 2041 and 4220 show where the space byte 20 remains. Use TRIM() first only if outside spaces carry no meaning. Binary strings are sliced in bytes. A cut in a text string can also fall within a combined accent or emoji sequence.

Extract parts of product codes

The common table expression (CTE) supplies example rows for this query. It shows a two-character prefix, a four-character suffix, whether the code starts with AB-, and the text after the final hyphen when one is present.

WITH sample_codes AS (
    SELECT 1 AS id, 'AB-2026-0042' AS product_code
    UNION ALL SELECT 2, 'CD-2026-0100'
    UNION ALL SELECT 3, 'AB-7-42'
    UNION ALL SELECT 4, 'X'
    UNION ALL SELECT 5, NULL
)
SELECT id, product_code,
       LEFT(product_code, 2) AS code_prefix,
       RIGHT(product_code, 4) AS code_suffix,
       product_code LIKE 'AB-%' AS has_ab_prefix,
       CASE WHEN LOCATE('-', product_code) > 0
            THEN SUBSTRING_INDEX(product_code, '-', -1)
       END AS dash_suffix
FROM sample_codes
ORDER BY id;

Rows 1 and 2 have suffixes 0042 and 0100. In row 3, the last four characters are 7-42 while the final hyphen-separated part is 42. Row 4 returns X as the fixed-length suffix even though it has no separator. Validate the format before relying on the extracted part. Row 5 preserves the missing value as NULL. For more examples, Compare character, byte, and bit lengths.

Use SUBSTRING() to start at a position within a string, or SUBSTRING_INDEX() to split on a delimiter. To filter rows by prefix, try WHERE product_code LIKE 'AB-%' and use EXPLAIN to check index use on the real table.