BigQuery · String function
BigQuery LENGTH
LENGTH(value) returns the number of characters in a STRING, or the number of bytes in a BYTES value. CHAR_LENGTH counts characters too; BYTE_LENGTH counts the UTF-8 bytes of a string, which differ for accented letters, non-Latin scripts and emoji.
LENGTH counts Unicode characters (code points) for STRING and bytes for BYTES. CHAR_LENGTH and CHARACTER_LENGTH are the same as LENGTH for strings. BYTE_LENGTH and its alias OCTET_LENGTH return the UTF-8 encoded size: 1 byte for ASCII, 2 for letters such as ï, up to 4 for emoji. For plain ASCII text all of them return the same number.
A code point is not always what a person sees as one character. A letter followed by a combining accent is two code points, and many emoji sequences are several. Normalize text with NORMALIZE (NFC) before you count if your data mixes composed and decomposed forms.
LENGTH returns 0 for an empty string and NULL for NULL, and it doesn't accept numbers or arrays: cast numbers to STRING, and use ARRAY_LENGTH for arrays. BigQuery has no LEN function.
Syntax
LENGTH(value)
CHAR_LENGTH(value)
CHARACTER_LENGTH(value)
BYTE_LENGTH(value)
OCTET_LENGTH(value)Arguments and return value
| Argument | Description |
|---|---|
value | A STRING or BYTES value. CHAR_LENGTH and CHARACTER_LENGTH take STRING only. |
Returns: INT64. NULL if value is NULL.
Examples
Characters vs bytes
SELECT
LENGTH('naïve') AS length,
CHAR_LENGTH('naïve') AS char_length,
BYTE_LENGTH('naïve') AS byte_length,
LENGTH('👍') AS emoji_length,
BYTE_LENGTH('👍') AS emoji_bytes;| length | char_length | byte_length | emoji_length | emoji_bytes |
|---|---|---|---|---|
| 5 | 5 | 6 | 1 | 4 |
Empty strings, NULLs and combining accents
'e\u0301' is e followed by a combining acute accent. NORMALIZE (NFC) composes it into the single character é.
SELECT
LENGTH('') AS empty_string,
LENGTH(CAST(NULL AS STRING)) AS null_string,
LENGTH('e\u0301') AS decomposed_e_acute,
LENGTH(NORMALIZE('e\u0301')) AS normalized;| empty_string | null_string | decomposed_e_acute | normalized |
|---|---|---|---|
| 0 | NULL | 2 | 1 |
Find values too long for a target column
Run this before loading into a system that limits names to 20 characters, or before casting to STRING(20).
WITH customers AS (
SELECT 1 AS customer_id, 'Ana' AS display_name UNION ALL
SELECT 2, 'Northwind Traders International Ltd'
)
SELECT customer_id, LENGTH(display_name) AS name_length
FROM customers
WHERE LENGTH(display_name) > 20
ORDER BY customer_id;| customer_id | name_length |
|---|---|
| 2 | 35 |
Common mistakes
- Using LENGTH to check byte limits. Systems that limit bytes (many databases, message queues, API fields) need BYTE_LENGTH;
LENGTH('naïve')is 5 but the value takes 6 bytes. - Calling LEN or DATALENGTH from SQL Server, or LEN from Snowflake. BigQuery has neither; use LENGTH or BYTE_LENGTH.
- Passing a number or an array.
LENGTH(12345)fails with No matching signature; useLENGTH(CAST(n AS STRING)), andARRAY_LENGTHfor arrays. - Counting NULL as zero.
LENGTH(NULL)is NULL, soWHERE LENGTH(col) = 0misses NULL values; useWHERE IFNULL(col, '') = ''to find both.
Cost and performance
- BigQuery bills on-demand queries by logical bytes read, and a STRING value counts as 2 bytes plus its UTF-8 size.
SUM(BYTE_LENGTH(col) + 2)therefore estimates how much a column contributes to bytes scanned. See BigQuery pricing.
FAQ
What is the difference between LENGTH, CHAR_LENGTH and BYTE_LENGTH in BigQuery?
For STRING, LENGTH and CHAR_LENGTH both count characters. BYTE_LENGTH counts UTF-8 bytes. For BYTES values, LENGTH counts bytes.
How do I get the length of an array?
Use ARRAY_LENGTH. LENGTH only accepts STRING and BYTES.
Why does LENGTH count an emoji as more than one character?
LENGTH counts Unicode code points. Simple emoji are one code point, but flags, skin tones and family emoji are sequences of several code points.
Related
Errors you may hit
More string functions
All BigQuery string functions · All functions · BigQuery error messages
