Function or operator · Text, numbers and dates

LENGTH

LENGTH measures the length of a value. In PostgreSQL and SQLite it counts the characters of a string; in MySQL it counts bytes, and CHAR_LENGTH counts characters.

LENGTH example

We want to find the longest product names:

SQL
SELECT name,
       LENGTH(name) AS characters
FROM products
ORDER BY characters DESC, name
LIMIT 6;

What to look for: The query shows each name and its number of characters. If two names have the same length, the second sort key name decides their order. Some databases need different functions to count bytes and characters.

Check the result with small data

We compare an acronym with a word.

Self-contained query · SQLite
WITH text_values(value) AS (VALUES ('SQL'), ('Course'))
SELECT value, LENGTH(value) AS characters
FROM text_values ORDER BY value;
Query result
valuecharacters
Course6
SQL3

In SQLite, LENGTH counts the characters in the strings supplied. On other engines, or when bytes are involved, the count can differ.

Check your understanding: What value would LENGTH('') return?

Show answer

0. The empty string exists and has no characters; LENGTH(NULL), on the other hand, returns NULL.

How LENGTH is used

If the argument is NULL, the result is NULL. An empty string has length zero.

Common mix-ups with LENGTH

Using MySQL's LENGTH as a universal character count

MySQL
SELECT LENGTH('Mouse'), CHAR_LENGTH('Mouse');

The two columns can differ: one counts bytes and the other counts characters. Pick the function that answers the question you are asking.

Key differences between engines

PostgreSQL distinguishes length for characters and octet_length for bytes. SQLite offers length for the characters of a string and octet_length in recent versions. MySQL uses LENGTH for bytes and CHAR_LENGTH for characters. Always check which unit your rule needs.

Choosing between characters, bytes and display width

Decide on the unit before measuring. A storage limit may be in bytes; validating a code may be about characters; a user interface may need units that match what the reader sees. Those measures do not always agree, especially with accents and emoji. A four-character string can take more than four bytes in UTF-8.

In SQLite, length on text counts code points up to any embedded null character, while on a BLOB it counts bytes. MySQL distinguishes LENGTH for bytes and CHAR_LENGTH for characters. SQL Server has other functions and quirks, such as how it treats trailing spaces. Do not swap function names on the assumption that the unit is built into a universal function.

Test ASCII text, accented text, an empty string and a null. An empty string has length zero; a missing value does not automatically become zero. Spaces are part of the text unless the engine has specific rules or you transform it explicitly. If the goal is to spot fields that only look empty, decide whether to strip spaces before measuring. For truncating interface text, a SQL count of code points may not respect characters that are displayed as one combined symbol.

Technical questions: SQLite: scalar functions (opens in a new tab).