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
SUBSTRING
Extracts a substring by 1-based start position and optional length. All three dialects support the comma-argument form; Postgres also accepts the SQL-standard FROM/FOR clauses and POSIX regex extraction.
TRIM
Removes matching characters from the start, end, or both sides of a string. Defaults to stripping spaces when no character set is supplied.
POSITION / INSTR
Returns the 1-based index of the first occurrence of substr in string, or 0 if not found. POSITION is SQL-standard (Postgres/MySQL); INSTR is the MySQL/SQLite shorthand.
CONCAT
Joins two or more strings end-to-end and returns a single string. CONCAT is portable across MySQL, Postgres 9.1+, and SQLite 3.44+; the standard || operator works in Postgres and SQLite but not MySQL unless PIPES_AS_CONCAT is set.
REPLACE
Replaces every occurrence of from_str inside source with to_str. Case-sensitive in Postgres and SQLite; MySQL follows the column's collation.
← All SQL functions · SQL cheat sheet · Window functions · PostgreSQL cheat sheet