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.
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
| Argument | Description |
|---|---|
value | The STRING or BYTES value to search. |
subvalue | The literal text to find, the same type as value. |
position | Optional INT64 start position, default 1. Negative values count from the end and search backwards (-1 is the last character). Can't be 0. |
occurrence | Optional 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;| last_slash | file_name | folder |
|---|---|---|
| 17 | events.json | data/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;| second_dash | second_dash_from_end | no_fifth_dash |
|---|---|---|
| 8 | 8 | 0 |
Overlapping matches count
SELECT INSTR('aaaa', 'aa', 1, 2) AS second_match;| second_match |
|---|
| 2 |
Common mistakes
- Passing
0as the position. It is an error (Position must be non-zero), unlike SUBSTR where 0 is treated as 1. Start at1. - 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, butLEFT(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.
Related
More string functions
All BigQuery string functions · All functions · BigQuery error messages
