Request a demo

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.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

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

ArgumentDescription
valueThe STRING or BYTES value.
positionINT64 start position. 1 is the first character; negative values count from the end (-1 is the last character); 0 is treated as 1.
lengthOptional 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;
Result
invoice_yearsequence_numbersequence_prefixdocument_type
20260004200INV

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;
Result
position_zerobefore_startafter_endcharacters_not_bytes
INVINVü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;
Result
emaildomain
maria.garcia@example.comexample.com
ops@data.example.orgdata.example.org

Common mistakes

  • Treating position as 0-based. SUBSTR(s, 0, 3) happens to work because 0 is treated as 1, but SUBSTR(s, 1, 3) is the intended form, and SUBSTR(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 form SUBSTRING(s, 2, 3).
  • Calling SUBSTR on a number. SUBSTR(12345, 2) fails with No matching signature; write SUBSTR(CAST(order_id AS STRING), 2).
  • Computing a negative length. SUBSTR(s, 1, STRPOS(s, '@') - 2) fails with Third argument in SUBSTR() cannot be negative on rows without @. Guard with GREATEST(..., 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.

Errors you may hit

More string functions

All BigQuery string functions · All functions · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.