Request a demo

BigQuery · String function

BigQuery INSTR

INSTR(value, subvalue[, position[, occurrence]]) returns the 1-based position of a substring, optionally starting at a given position or finding the nth match. INSTR(path, '/', -1) finds the last slash; it returns 0 when there is no match.

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

INSTR extends STRPOS with two optional arguments. position sets where the search starts; a negative value starts that many characters from the end and searches backwards, which is how you find the last occurrence of a delimiter. occurrence picks the nth match instead of the first.

The returned position is always counted from the start of the string, even when you search backwards. Matches can overlap when occurrence is greater than 1: in aaaa, the second aa starts at position 2.

INSTR returns 0 when there is no match, when occurrence exceeds the number of matches, or when position is past the end of the string. It returns NULL if any argument is NULL, and an error if position is 0 or occurrence is 0 or negative.

Syntax

INSTR(value, subvalue[, position[, occurrence]])

Arguments and return value

ArgumentDescription
valueThe STRING or BYTES value to search.
subvalueThe literal text to find, the same type as value.
positionOptional INT64 start position, default 1. Negative values count from the end and search backwards (-1 is the last character). Can't be 0.
occurrenceOptional INT64, default 1: which match to return. Must be positive.

Returns: INT64: the 1-based position of the match, or 0 if there is none.

Examples

File name and folder from a path

SELECT
  INSTR('data/raw/2026/11/events.json', '/', -1) AS last_slash,
  SUBSTR('data/raw/2026/11/events.json', INSTR('data/raw/2026/11/events.json', '/', -1) + 1) AS file_name,
  LEFT('data/raw/2026/11/events.json', INSTR('data/raw/2026/11/events.json', '/', -1) - 1) AS folder;
Result
last_slashfile_namefolder
17events.jsondata/raw/2026/11

The nth occurrence

SELECT
  INSTR('2026-11-23-backfill', '-', 1, 2) AS second_dash,
  INSTR('2026-11-23-backfill', '-', -1, 2) AS second_dash_from_end,
  INSTR('2026-11-23-backfill', '-', 1, 5) AS no_fifth_dash;
Result
second_dashsecond_dash_from_endno_fifth_dash
880

Overlapping matches count

SELECT INSTR('aaaa', 'aa', 1, 2) AS second_match;
Result
second_match
2

Common mistakes

  • Passing 0 as the position. It is an error (Position must be non-zero), unlike SUBSTR where 0 is treated as 1. Start at 1.
  • Porting MySQL's LOCATE(substr, str, pos). BigQuery has no LOCATE, and INSTR takes the string first: INSTR(str, substr, pos). The four-argument form follows Oracle's INSTR.
  • Using a position without checking for 0. SUBSTR(path, INSTR(path, '/', -1) + 1) returns the whole path when there is no slash, which is usually what you want, but LEFT(path, INSTR(path, '/', -1) - 1) fails on such rows.
  • Expecting non-overlapping occurrences. INSTR counts overlapping matches (INSTR('aaaa', 'aa', 1, 2) is 2), while REGEXP_EXTRACT_ALL and REPLACE work on non-overlapping matches.

FAQ

How do I find the last occurrence of a character in BigQuery?

Use INSTR(value, '/', -1). It searches backwards from the last character and returns the position counted from the start.

How do I get the text after the last delimiter?

SUBSTR(value, INSTR(value, '/', -1) + 1), or ARRAY_LAST(SPLIT(value, '/')).

What is the difference between INSTR and STRPOS?

INSTR(value, sub) with two arguments is the same as STRPOS(value, sub). INSTR also takes a start position and an occurrence number.

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.