Skip to content
SQLSimplified
string

LENGTH()

Returns the number of characters in a string.

Description

LENGTH counts the number of characters in a string. It's useful for validating data (like checking that a code has a fixed number of characters), sorting by how long a value is, or filtering out unusually short or long text.

Syntax

LENGTH(string)

Parameters

NameDescriptionOptional
stringThe text value or column to measure.No

Return Type

LENGTH returns a BIGINT, the character count.

Examples

Loading playground environment...
Loading playground environment...

LENGTH counts characters, not bytes

For most text this matches what you'd expect, but with multi-byte characters (accents, emoji, non-Latin scripts), LENGTH still counts characters, not raw storage bytes, which is usually what you want.

Common Mistakes

  • Confusing LENGTH with counting words. LENGTH('The Matrix') returns 10 (including the space), not the number of words.
  • Forgetting that leading or trailing spaces count. 'Avatar ' with a trailing space has a different length than 'Avatar', which can cause confusing off-by-one results if the data has inconsistent spacing.
  • Applying LENGTH to a NULL value and expecting 0. LENGTH(NULL) returns NULL, not 0, since there's no string to measure.

See also: SUBSTRING, CONCAT, UPPER.

Cite this resource

SQLSimplified. "LENGTH() SQL Function". Available at: https://sqlsimplified.online/reference/length