string

LENGTH / CHAR_LENGTH

Returns the number of characters in a string (CHAR_LENGTH) or the number of bytes (LENGTH in MySQL). Postgres and SQLite LENGTH return character count for text, matching CHAR_LENGTH.

LENGTH(string)  |  CHAR_LENGTH(string)

Parameters / Modifiers

Parameter Purpose
CHAR_LENGTH Character count regardless of multi-byte encoding (portable)
OCTET_LENGTH Bytes occupied by the string
BIT_LENGTH Length in bits (Postgres/MySQL)

Examples

SELECT LENGTH('hello');

Returns 5 in every dialect

SELECT LENGTH('café');

MySQL returns 5 (bytes); Postgres/SQLite return 4 (chars)

SELECT CHAR_LENGTH('café');

Always 4 — safest for multibyte input

SELECT email FROM users WHERE LENGTH(email) > 100;

Find suspiciously long email addresses

Dialect notes / Gotcha

MySQL LENGTH counts bytes, not characters — use CHAR_LENGTH for user-visible length. LENGTH(NULL) returns NULL, not 0.

Related functions

← All SQL functions · SQL cheat sheet · Window functions · PostgreSQL cheat sheet