MariaDB LENGTH, BIT_LENGTH, and CHAR_LENGTH: bytes, bits, and characters
In MariaDB, CHAR_LENGTH() counts characters, OCTET_LENGTH() counts bytes, and BIT_LENGTH() counts bits. LENGTH() counts bytes in ordinary mode and characters in Oracle compatibility mode. UTF-8 strings can have different lengths in those units.
Choose the unit: characters, bytes, or bits
| Function | Unit | Use |
|---|---|---|
| LENGTH() | Bytes in ordinary mode, characters in Oracle mode | Measure length with the SQL mode in mind |
| OCTET_LENGTH() | Bytes | Check encoded data or an external byte budget |
| BIT_LENGTH() | Bits | Encoded byte count × 8 |
| CHAR_LENGTH() | Characters | Count database characters or determine a character position |
CHAR_LENGTH() and CHARACTER_LENGTH() are aliases. BIT_LENGTH() is the byte count multiplied by eight. For an explicit byte count regardless of SQL mode, use OCTET_LENGTH(). MariaDB Docs · MariaDB LENGTH() MariaDB Docs · MariaDB CHAR_LENGTH() MariaDB Docs · MariaDB BIT_LENGTH() MariaDB Docs · MariaDB OCTET_LENGTH()
Measure a string containing Korean and ASCII
SELECT LENGTH(_utf8mb4'A1#가') AS byte_length,
BIT_LENGTH(_utf8mb4'A1#가') AS bit_length,
CHAR_LENGTH(_utf8mb4'A1#가') AS character_count,
OCTET_LENGTH(_utf8mb4'A1#가') AS explicit_bytes;
Run the first query without a table. Use UTF-8 for the SQL file and client to reproduce the result. In ordinary mode the values are 6, 48, 4, and 6. A, 1, and # occupy one byte each. The Korean character 가 occupies three bytes in UTF-8. There are four characters in total.
SELECT CHARSET(_utf8mb4'A1#가') AS sample_charset,
HEX(_utf8mb4'A1#가') AS sample_bytes,
@@session.character_set_connection AS connection_charset,
@@session.sql_mode AS current_sql_mode;
The next query reports utf8mb4 for sample_charset and 413123EAB080 for sample_bytes. The remaining fields depend on the connection. If the output differs, check the file, connection, column character set and Oracle mode. This byte count measures the value. On-disk row and index overhead is a separate matter.
Distinguish empty strings, NULL, and spaces
SELECT LENGTH('') AS empty_bytes,
BIT_LENGTH('') AS empty_bits,
CHAR_LENGTH('') AS empty_chars,
LENGTH(NULL) AS null_bytes,
BIT_LENGTH(NULL) AS null_bits,
CHAR_LENGTH(NULL) AS null_chars;
SELECT CHAR_LENGTH(' a ') AS with_spaces,
CHAR_LENGTH(TRIM(' a ')) AS after_trim;
In ordinary mode, an empty string has zero characters, bytes, and bits. A NULL input gives NULL. The example with a space on each side of a has three characters, or one after removing those spaces with TRIM. Only map missing values to zero or strip spaces when your data rules call for it.
Compare visible graphemes with database character counts
The next four examples use hexadecimal literals to specify the exact UTF-8 bytes. A Unicode grapheme cluster is a sequence of code points that can appear as one character to a reader. MariaDB’s CHAR_LENGTH() counts encoded characters instead of applying grapheme segmentation. MariaDB Docs · Unicode UAX #29
SELECT sample_no, HEX(value_text) AS bytes_hex,
OCTET_LENGTH(value_text) AS bytes_count,
BIT_LENGTH(value_text) AS bits_count,
CHAR_LENGTH(value_text) AS characters_count
FROM (
SELECT 1 AS sample_no, CONVERT(0xEAB080 USING utf8mb4) AS value_text
UNION ALL
SELECT 2, CONVERT(0xE18480E185A1 USING utf8mb4)
UNION ALL
SELECT 3, CONVERT(0xF09F9880 USING utf8mb4)
UNION ALL
SELECT 4, CONVERT(0xF09F91A9E2808DF09F92BB USING utf8mb4)
) AS unicode_samples ORDER BY sample_no;
The first row, precomposed 가, has 3 bytes, 24 bits, and 1 character. The decomposed form in the second row has 6 bytes, 48 bits, and 2 characters. A single emoji in row three has 4 bytes, 32 bits, and 1 character. The joined emoji in row four has 11 bytes, 88 bits, and 3 characters, even where it displays as one symbol. Apply display limits with Unicode grapheme-aware application logic.
Apply character and byte conditions to a column
The final examples use the learning practice database with CREATE, INSERT, and SELECT privileges. If the example table already exists, use a fresh practice database. The first query reports character and byte pairs by id: (3,3), (3,9), (0,0), (NULL,NULL), and (4,4).
USE learning;
CREATE TABLE string_length_demo (
id INT PRIMARY KEY,
label VARCHAR(100) CHARACTER SET utf8mb4 NULL
);
INSERT INTO string_length_demo VALUES
(1, 'abc'), (2, '가나다'), (3, ''), (4, NULL), (5, ' ab ');
SELECT id, CHAR_LENGTH(label) AS chars,
OCTET_LENGTH(label) AS bytes
FROM string_length_demo ORDER BY id;
SELECT id, label
FROM string_length_demo
WHERE CHAR_LENGTH(label) >= 3
ORDER BY id;
SELECT id, label
FROM string_length_demo
WHERE OCTET_LENGTH(label) > 6
ORDER BY id;
SELECT id FROM string_length_demo WHERE label IS NULL;
SELECT CHAR_LENGTH('007') AS text_digits,
CHAR_LENGTH(CAST(7 AS CHAR)) AS numeric_digits;
Rows with at least three characters are IDs 1, 2, and 5. Only ID 2 exceeds six bytes, and IS NULL finds ID 4. The last SELECT counts the text 007 as three characters and the number 7, converted to text, as one. Decide whether you need display length, database character count, or a byte budget for an external format. Also check row and index limits when designing a VARCHAR column.




