Return the number of characters in the input string.
LENGTH(str)
## Overview Returns the number of characters in the input string. In DeltaForge, LENGTH is a character count, not a byte count, so multi-byte UTF-8 characters each contribute 1 to the result. This aligns with CHAR_LENGTH and CHARACTER_LENGTH and matches how most application code counts characters. Use LENGTH for validation (enforcing maximum length for a name, ensuring a code is exactly N characters), for computing summary statistics, and as input to SUBSTRING-based slicing. For byte-accurate storage estimates, use OCTET_LENGTH; for bit counts, use BIT_LENGTH. ## Behavior - Returns NULL when the input is NULL. - Returns 0 for an empty string. - Each Unicode code point counts as one character. Multi-byte UTF-8 sequences are not double-counted. - Combining sequences (for example a base letter plus an accent mark) count as the number of code points, not grapheme clusters. 'e\u0301' has LENGTH 2 even though it visually renders as one character. - LENGTH never returns a negative value. - CHAR_LENGTH and CHARACTER_LENGTH are strict synonyms for LENGTH when applied to string types. ## Compatibility - Conforms to the SQL standard definition when applied to character strings. - For VARBINARY or BYTEA inputs, some dialects define LENGTH as a byte count. DeltaForge requires explicit use of OCTET_LENGTH for byte counting.
| Name | Type | Description |
|---|---|---|
str | Specifies the input string whose character count is returned. Accepts any VARCHAR or CHAR value. |
-- Basic ASCII length
SELECT LENGTH('hello'); -- 5
-- Empty string
SELECT LENGTH(''); -- 0
-- NULL propagates
SELECT LENGTH(CAST(NULL AS VARCHAR)); -- NULL
-- Multi-byte characters count as one character each
SELECT LENGTH('éèê'); -- 3
-- Validate that no customer name exceeds 100 characters
SELECT customer_id, full_name
FROM retail.customers.profiles
WHERE LENGTH(full_name) > 100;
-- Average description length per category
SELECT category, AVG(LENGTH(description)) AS avg_len
FROM retail.products.catalog
GROUP BY category;