SQL Get Length of String: LEN, LENGTH, DATALENGTH Reference
TL;DR: To get the length of a string in SQL, use LEN(col) in SQL Server and CHAR_LENGTH(col) (or LENGTH(col)) in PostgreSQL, MySQL, Oracle, BigQuery and SQLite. The trap is that LENGTH counts bytes in MySQL but characters everywhere else, and LEN ignores trailing spaces. If you would rather ask "which customer names are longer than 50 characters?" in plain English, AI for Database writes and runs the right function for your engine and shows you the SQL.
Try AI for Database free at aifordatabase.com
The function that returns string length depends on your database. In SQL Server it is LEN() for characters and DATALENGTH() for bytes. In PostgreSQL, LENGTH(), CHAR_LENGTH() and CHARACTER_LENGTH() all count characters and OCTET_LENGTH() counts bytes. In MySQL, CHAR_LENGTH() counts characters while LENGTH() counts bytes, so LENGTH('€') returns 3. Oracle uses LENGTH() for characters and LENGTHB() for bytes, BigQuery uses LENGTH() or CHAR_LENGTH() with BYTE_LENGTH() for bytes, and SQLite uses length(). Every one of them returns NULL when the input is NULL.
SQL string length functions: lookup table
| Database | Characters | Bytes | Trailing spaces counted? | Empty string '' |
|---|---|---|---|---|
| SQL Server | LEN(s) | DATALENGTH(s) | No for LEN, yes for DATALENGTH | 0 |
| PostgreSQL | LENGTH(s), CHAR_LENGTH(s) | OCTET_LENGTH(s) | Yes for text/varchar, no for char(n) | 0 |
| MySQL | CHAR_LENGTH(s) | LENGTH(s), OCTET_LENGTH(s) | Yes (varchar) | 0 |
| Oracle | LENGTH(s) | LENGTHB(s) | Yes | NULL ('' is NULL) |
| BigQuery | LENGTH(s), CHAR_LENGTH(s) | BYTE_LENGTH(s) | Yes | 0 |
| SQLite | length(s) | octet_length(s) (3.43+) | Yes | 0 |
Sources: SQL Server LEN, PostgreSQL string functions, MySQL string functions, Oracle LENGTH, BigQuery string functions, SQLite core functions.

Which function works on every engine?
No single function works everywhere. CHAR_LENGTH comes closest: PostgreSQL, MySQL and BigQuery all support it and it always means characters, while SQL Server needs LEN.
-- PostgreSQL, MySQL, BigQuery
SELECT name, CHAR_LENGTH(name) AS name_length
FROM customers
ORDER BY name_length DESC
LIMIT 10;-- SQL Server
SELECT TOP 10 name, LEN(name) AS name_length
FROM customers
ORDER BY name_length DESC;To filter on length, put the function in WHERE, for example to find postcodes with the wrong number of characters:
SELECT id, postcode
FROM addresses
WHERE CHAR_LENGTH(postcode) <> 5;Wrapping a column in a function stops a plain index on that column from being used, so on a large table index the expression (CREATE INDEX ON addresses ((char_length(postcode))) in PostgreSQL) or store the length in a computed column.
SQL Server: LEN vs DATALENGTH
LEN returns the number of characters, excluding trailing spaces. DATALENGTH returns the number of bytes and does not trim anything. Microsoft's docs say it directly: "LEN excludes trailing spaces. If that is a problem, consider using the DATALENGTH function."
SELECT
LEN('abc ') AS len_varchar, -- 3
DATALENGTH('abc ') AS bytes_varchar, -- 6
LEN(N'abc') AS len_nvarchar, -- 3
DATALENGTH(N'abc') AS bytes_nvarchar; -- 6, two bytes per characterWhat is the difference between LEN and DATALENGTH?
LEN counts characters and drops trailing spaces; DATALENGTH counts storage bytes, keeps trailing spaces and doubles for nvarchar (UTF-16). To count characters including trailing spaces, use LEN(col + 'x') - 1 or DATALENGTH(col) / 2 for nvarchar. LEN returns bigint for varchar(max), nvarchar(max) and varbinary(max) and int otherwise. Leading spaces are always counted.
PostgreSQL: LENGTH, CHAR_LENGTH, OCTET_LENGTH
In PostgreSQL, length(text), char_length and character_length are the same thing: number of characters. octet_length returns bytes, which differs for any non-ASCII text in a UTF-8 database.
SELECT length('café'), octet_length('café'); -- 4, 5
SELECT length('ab '::char(3)); -- 2, char(n) padding is ignored
SELECT length('ab '::varchar); -- 3The char(n) case surprises people: PostgreSQL treats trailing spaces in character(n) as insignificant, so they are removed before measuring (PostgreSQL character types). More PostgreSQL patterns are in our [PostgreSQL guide](/databases/postgresql/).
MySQL: CHAR_LENGTH vs LENGTH
In MySQL, LENGTH() returns the length in bytes and CHAR_LENGTH() the length in characters. OCTET_LENGTH() is a synonym for LENGTH(). With the default utf8mb4 character set, an accented letter is 2 bytes, € is 3 and most emoji are 4.
SELECT LENGTH('€'), CHAR_LENGTH('€'); -- 3, 1
SELECT name FROM users WHERE CHAR_LENGTH(name) > 50;If a "maximum 50 characters" check uses LENGTH, it will reject valid names with accents. Our [MySQL page](/databases/mysql/) covers connecting MySQL to plain-English questions.
Why does LENGTH return a different number for Unicode text?
Because LENGTH counts bytes in MySQL (and LENGTHB, OCTET_LENGTH, DATALENGTH, BYTE_LENGTH do elsewhere), and Unicode characters outside ASCII take 2 to 4 bytes in UTF-8. Use the character function (CHAR_LENGTH, Oracle LENGTH, SQL Server LEN) when you mean characters.
Oracle: LENGTH and LENGTHB
Oracle's LENGTH returns characters in the input character set, LENGTHB returns bytes, and LENGTHC, LENGTH2 and LENGTH4 count Unicode complete characters, UCS2 and UCS4 code points. Oracle stores an empty string as NULL, so LENGTH('') is NULL, not 0.
SELECT LENGTH('café'), LENGTHB('café') FROM dual;
SELECT NVL(LENGTH(middle_name), 0) AS middle_len FROM employees;BigQuery and SQLite
BigQuery LENGTH returns characters for a STRING and bytes for a BYTES value; CHAR_LENGTH and CHARACTER_LENGTH are character synonyms and BYTE_LENGTH returns bytes. SQLite length(X) returns characters for text (stopping at the first NUL character) and bytes for a blob; octet_length(X) was added in SQLite 3.43.0.
-- BigQuery
SELECT LENGTH('café') AS chars, BYTE_LENGTH('café') AS bytes; -- 4, 5
-- SQLite
SELECT length('café'), octet_length('café'); -- 4, 5Does LEN count trailing spaces?
No. SQL Server's LEN excludes trailing spaces, so LEN('abc ') is 3. LENGTH in PostgreSQL (for text and varchar), MySQL, Oracle, BigQuery and SQLite does count them. PostgreSQL char(n) and MySQL CHAR(n) columns strip trailing pad spaces when you read them, so their lengths look trimmed too.

AI for Database: length checks in plain English
AI for Database connects to PostgreSQL, MySQL, SQL Server, Oracle, SQLite, BigQuery and more, and answers questions in plain English. Ask "how many product descriptions are over 160 characters?" and it picks LEN or CHAR_LENGTH for the engine you connected, runs the query read-only and shows the SQL beside the answer, so you can check that it measured characters, not bytes. You can pin the result to a dashboard that refreshes on a schedule, or set an action workflow that sends a Slack alert or email when new rows break a length rule. It starts free with sample data, and Pro is $10 per user per month ([pricing](/pricing/)). For more counting patterns, see [SQL COUNT examples](/blog/sql-count-statement-examples/).
Frequently Asked Questions
How do I get the length of a string in SQL?
Use LEN(s) in SQL Server and CHAR_LENGTH(s) in PostgreSQL, MySQL and BigQuery; LENGTH(s) works for characters in PostgreSQL, Oracle, BigQuery and SQLite.
What is the difference between LEN and DATALENGTH?
LEN returns characters without trailing spaces; DATALENGTH returns bytes with trailing spaces, and two bytes per character for nvarchar.
What is the SQL length of a varchar column?
CHAR_LENGTH(col) (or LEN(col) in SQL Server) gives the length of the stored value. The declared maximum, such as varchar(255), is in information_schema.columns.character_maximum_length.
Why does LENGTH return a different number for Unicode text?
In MySQL LENGTH counts bytes, and non-ASCII characters take 2 to 4 bytes in UTF-8. Use CHAR_LENGTH for characters.
Does LEN count trailing spaces?
No, SQL Server LEN ignores trailing spaces. Use DATALENGTH or LEN(col + 'x') - 1 to include them.
What does LENGTH return for NULL?
NULL in every engine. Wrap it in COALESCE(LENGTH(col), 0) when you need 0. Want answers without picking the right function for each engine? [Try AI for Database free at aifordatabase.com](https://app.aifordatabase.com/signup).