BigQuery · String function
BigQuery STRPOS
STRPOS(value, subvalue) returns the 1-based position of the first occurrence of subvalue, or 0 if it isn't found: STRPOS('ana@example.com', '@') returns 4. Use it with SUBSTR to cut strings at a delimiter.
STRPOS searches value for the literal text subvalue and returns where the first match starts, counting characters from 1. It returns 0 when there is no match and NULL when either argument is NULL. The search is case-sensitive and doesn't support wildcards.
The most common use is as an input to SUBSTR, LEFT or RIGHT: find the @ in an email, then take the part before or after it. Because a missing delimiter returns 0, check the result before you compute a length from it.
For the second or the last occurrence, use INSTR, which adds position and occurrence arguments. To test only whether text is present, STRPOS(value, x) > 0 works, but CONTAINS_SUBSTR (case-insensitive) or LIKE '%x%' read more clearly.
Syntax
STRPOS(value, subvalue)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. |
Returns: INT64: the 1-based position of the first match, 0 if there is none, NULL if an argument is NULL.
Examples
Position, no match and case sensitivity
The capital E at position 1 doesn't match e.
SELECT
STRPOS('ana@example.com', '@') AS at_sign,
STRPOS('ana@example.com', '#') AS missing,
STRPOS('Example', 'e') AS lowercase_e;| at_sign | missing | lowercase_e |
|---|---|---|
| 4 | 0 | 7 |
Split an email into user and domain
Without the IF, LEFT(email, -1) would fail on the row without @.
WITH users AS (
SELECT 'maria.garcia@example.com' AS email UNION ALL
SELECT 'not-an-email'
)
SELECT
email,
IF(STRPOS(email, '@') > 0, LEFT(email, STRPOS(email, '@') - 1), NULL) AS user_name,
IF(STRPOS(email, '@') > 0, SUBSTR(email, STRPOS(email, '@') + 1), NULL) AS domain
FROM users
ORDER BY email;| user_name | domain | |
|---|---|---|
| maria.garcia@example.com | maria.garcia | example.com |
| not-an-email | NULL | NULL |
Common mistakes
- Using the result as a length without checking for 0.
LEFT(email, STRPOS(email, '@') - 1)fails withSecond argument in LEFT() cannot be negativeon rows without@. - Porting the argument order from SQL Server.
CHARINDEX(substring, string)takes the substring first; STRPOS takes the string first, like PostgreSQL's STRPOS and MySQL'sINSTR(str, substr). - Expecting case-insensitive search. Compare
STRPOS(LOWER(value), 'abc')if case shouldn't matter. - Looking for POSITION, CHARINDEX or LOCATE. BigQuery has STRPOS and INSTR.
FAQ
How do I find the position of a character in a string in BigQuery?
Use STRPOS(value, 'x') for the first occurrence. For the last occurrence, use INSTR(value, 'x', -1).
What does STRPOS return when the substring isn't found?
0, not NULL. It returns NULL only when one of the arguments is NULL.
What is the difference between STRPOS and INSTR?
STRPOS takes two arguments and finds the first match. INSTR also accepts a start position (negative to search backwards) and an occurrence number.
Related
More string functions
All BigQuery string functions · All functions · BigQuery error messages
