Return the number of characters (or bytes) in a string.
Returns the number of characters (or bytes, in some databases) in a string. CHARACTER_LENGTH, CHAR_LENGTH, LEN, and LENGTH have slight differences across databases. NULL input returns NULL.
| Database System | Support Status | Since Version | Notes |
|---|---|---|---|
| MySQL | ✓ Supported | all | LENGTH() returns byte length (important for multi-byte UTF-8 strings). CHAR_LENGTH() / CHARACTER_LENGTH() returns character count. For emoji/CJK text, use CHAR_LENGTH(). |
| PostgreSQL | ✓ Supported | all | LENGTH() returns character count (not bytes) for text/varchar. OCTET_LENGTH() returns byte length. BIT_LENGTH() returns bit count. For bytea columns, LENGTH() returns byte count. |
| SQL Server | ✓ Supported | all | LEN() returns character count, excluding trailing spaces. DATALENGTH() returns byte length (NVARCHAR = 2 bytes per char). LEN() does NOT count trailing spaces — use DATALENGTH() if that matters. |
| Oracle | ✓ Supported | all | LENGTH() returns character count based on database character set. LENGTHB() returns byte length. LENGTHC() uses Unicode complete character count. |
| SQLite | ✓ Supported | all | LENGTH() returns character count for TEXT values. For BLOB values, returns byte count. SQLite stores text as UTF-8 or UTF-16 internally. |
The biggest trap: SQL Server uses LEN() not LENGTH() and silently ignores trailing spaces. MySQL's LENGTH() returns bytes, not characters — always use CHAR_LENGTH() for multi-byte text in MySQL. PostgreSQL and SQLite's LENGTH() correctly returns character count.
LENGTH() returns byte length (important for multi-byte UTF-8 strings). CHAR_LENGTH() / CHARACTER_LENGTH() returns character count. For emoji/CJK text, use CHAR_LENGTH().
LENGTH() returns character count (not bytes) for text/varchar. OCTET_LENGTH() returns byte length. BIT_LENGTH() returns bit count. For bytea columns, LENGTH() returns byte count.
LEN() returns character count, excluding trailing spaces. DATALENGTH() returns byte length (NVARCHAR = 2 bytes per char). LEN() does NOT count trailing spaces — use DATALENGTH() if that matters.
LENGTH() returns character count based on database character set. LENGTHB() returns byte length. LENGTHC() uses Unicode complete character count.
LENGTH() returns character count for TEXT values. For BLOB values, returns byte count. SQLite stores text as UTF-8 or UTF-16 internally.