BigQuery · String function
BigQuery LEFT
LEFT(value, length) returns the first length characters of a string (or bytes of a BYTES value): LEFT('4111111111111111', 4) returns 4111. If the value is shorter, LEFT returns it whole; a negative length is an error.
LEFT is a readable shortcut for SUBSTR(value, 1, length). For STRING it counts characters, for BYTES it counts bytes. A length of 0 returns an empty string, a length longer than the value returns the whole value, and NULL in either argument returns NULL.
The only failure mode is a negative length, which raises Second argument in LEFT() cannot be negative. This shows up when the length is computed, for example from STRPOS on a row where the delimiter is missing, so guard computed lengths.
For the end of a string, use RIGHT. For a slice in the middle, use SUBSTR.
Syntax
LEFT(value, length)Arguments and return value
| Argument | Description |
|---|---|
value | The STRING or BYTES value. |
length | INT64 number of characters (STRING) or bytes (BYTES) to return. Must not be negative. |
Returns: STRING or BYTES, the same type as value.
Examples
Prefixes and short values
SELECT
LEFT('4111111111111111', 4) AS card_prefix,
LEFT('2026-11-23T08:15:00', 10) AS date_part,
LEFT('EU', 5) AS shorter_than_length,
LEFT('Zürich', 2) AS characters_not_bytes;| card_prefix | date_part | shorter_than_length | characters_not_bytes |
|---|---|---|---|
| 4111 | 2026-11-23 | EU | Zü |
Group by a code prefix
WITH products AS (
SELECT 'ELEC-1001' AS sku, 120 AS revenue UNION ALL
SELECT 'ELEC-1002', 80 UNION ALL
SELECT 'HOME-2001', 45
)
SELECT LEFT(sku, 4) AS product_line, SUM(revenue) AS revenue
FROM products
GROUP BY product_line
ORDER BY product_line;| product_line | revenue |
|---|---|
| ELEC | 200 |
| HOME | 45 |
Common mistakes
- Computing a negative length.
LEFT(email, STRPOS(email, '@') - 1)fails on rows without@because STRPOS returns 0. Wrap it:IF(STRPOS(email, '@') > 0, LEFT(...), NULL). - Using LEFT on a number or date.
LEFT(20261123, 4)fails with No matching signature; CAST to STRING first, or use EXTRACT for date parts. - Expecting padding. LEFT never adds characters to short values; use RPAD to force a fixed width.
FAQ
How do I get the first N characters of a string in BigQuery?
LEFT(value, n) or SUBSTR(value, 1, n). Both return the whole string when it is shorter than n.
Does BigQuery have LEFT and RIGHT functions?
Yes. LEFT(value, length) and RIGHT(value, length) work on STRING and BYTES.
How do I remove the first N characters?
Use SUBSTR(value, n + 1). For example, SUBSTR('INV-00042', 5) returns 00042.
Related
Errors you may hit
More string functions
All BigQuery string functions · All functions · BigQuery error messages
