BigQuery · String function
BigQuery SUBSTR
SUBSTR(value, position[, length]) returns part of a string: SUBSTR('INV-2026-00042', 5, 4) returns 2026. Positions start at 1, and a negative position counts from the end. SUBSTRING is an alias.
SUBSTR extracts a substring by position. Positions are 1-based and count characters for STRING and bytes for BYTES, so multi-byte characters such as ü count as one. Without length, the substring runs to the end of the value.
A negative position counts from the end: SUBSTR(code, -3) returns the last three characters. A position of 0 is treated as 1, and a position before the start of the string is also moved to 1. A position after the end returns an empty string, and a length longer than the rest of the string just returns what is left. Only a negative length is an error.
SUBSTRING is an alias with the same arguments, but the SQL standard form SUBSTRING(x FROM 2 FOR 3) is a syntax error in BigQuery. For the first or last n characters, LEFT and RIGHT are easier to read. To cut at a delimiter rather than a fixed position, combine SUBSTR with STRPOS or INSTR, or use SPLIT.
Syntax
SUBSTR(value, position[, length])
SUBSTRING(value, position[, length])Arguments and return value
| Argument | Description |
|---|---|
value | The STRING or BYTES value. |
position | INT64 start position. 1 is the first character; negative values count from the end (-1 is the last character); 0 is treated as 1. |
length | Optional INT64 maximum number of characters (or bytes) to return. Must not be negative. |
Returns: STRING or BYTES, the same type as value. NULL if any argument is NULL.
Examples
Fixed positions and negative positions
SELECT
SUBSTR('INV-2026-00042', 5, 4) AS invoice_year,
SUBSTR('INV-2026-00042', -5) AS sequence_number,
SUBSTR('INV-2026-00042', -5, 2) AS sequence_prefix,
SUBSTRING('INV-2026-00042', 1, 3) AS document_type;| invoice_year | sequence_number | sequence_prefix | document_type |
|---|---|---|---|
| 2026 | 00042 | 00 | INV |
Out-of-range positions don't fail
after_end is an empty string, not NULL.
SELECT
SUBSTR('INV-2026-00042', 0, 3) AS position_zero,
SUBSTR('INV-2026-00042', -50, 3) AS before_start,
SUBSTR('INV-2026-00042', 50) AS after_end,
SUBSTR('Zürich', 2, 3) AS characters_not_bytes;| position_zero | before_start | after_end | characters_not_bytes |
|---|---|---|---|
| INV | INV | üri |
Cut at a delimiter: the domain of an email
WITH users AS (
SELECT 'maria.garcia@example.com' AS email UNION ALL
SELECT 'ops@data.example.org'
)
SELECT email, SUBSTR(email, STRPOS(email, '@') + 1) AS domain
FROM users
ORDER BY email;| domain | |
|---|---|
| maria.garcia@example.com | example.com |
| ops@data.example.org | data.example.org |
Common mistakes
- Treating position as 0-based.
SUBSTR(s, 0, 3)happens to work because 0 is treated as 1, butSUBSTR(s, 1, 3)is the intended form, andSUBSTR(s, 2)skips only the first character. - Porting negative positions from PostgreSQL. In PostgreSQL a negative start does not count from the end; in BigQuery (as in MySQL and Snowflake)
SUBSTR(s, -3)returns the last three characters. - Using the standard
SUBSTRING(s FROM 2 FOR 3)syntax. BigQuery only accepts the comma formSUBSTRING(s, 2, 3). - Calling SUBSTR on a number.
SUBSTR(12345, 2)fails with No matching signature; writeSUBSTR(CAST(order_id AS STRING), 2). - Computing a negative length.
SUBSTR(s, 1, STRPOS(s, '@') - 2)fails withThird argument in SUBSTR() cannot be negativeon rows without@. Guard withGREATEST(..., 0)or a CASE.
FAQ
What is the difference between SUBSTR and SUBSTRING in BigQuery?
None. SUBSTRING is an alias for SUBSTR with the same arguments: SUBSTRING(value, position, length).
How do I get the last N characters of a string in BigQuery?
Use SUBSTR(value, -n) or RIGHT(value, n). Both return the whole string when it is shorter than n.
How do I get the substring before or after a character?
Find the character with STRPOS: SUBSTR(email, 1, STRPOS(email, '@') - 1) is the part before @, SUBSTR(email, STRPOS(email, '@') + 1) the part after it. SPLIT(email, '@')[SAFE_OFFSET(0)] is a shorter alternative.
Does SUBSTR count characters or bytes?
Characters for STRING values and bytes for BYTES values. Use SUBSTR(CAST(value AS BYTES), ...) only when you really need byte offsets.
Related
Errors you may hit
More string functions
All BigQuery string functions · All functions · BigQuery error messages
