BigQuery · String function
BigQuery STARTS_WITH
STARTS_WITH(value, prefix) returns TRUE if a string begins with prefix: STARTS_WITH(table_name, 'tmp_') is TRUE for tmp_orders. It is case-sensitive and, unlike LIKE 'tmp_%', treats _ and % as plain characters.
STARTS_WITH compares the beginning of value with prefix and returns a BOOL. The comparison is exact and case-sensitive. An empty prefix always returns TRUE, and a NULL argument returns NULL, which a WHERE clause treats as not matching.
The main advantage over LIKE 'prefix%' is escaping. In LIKE, _ matches any single character and % any sequence, so table_name LIKE 'tmp_%' also matches tmpx_orders. STARTS_WITH treats every character literally, which suits table names, GCS paths and codes that contain underscores.
For a case-insensitive check, lowercase the value: STARTS_WITH(LOWER(email), 'admin'). For suffixes, use ENDS_WITH; for text anywhere in the string, use CONTAINS_SUBSTR or LIKE.
Syntax
STARTS_WITH(value, prefix)Arguments and return value
| Argument | Description |
|---|---|
value | The STRING or BYTES value to test. |
prefix | The prefix to look for, the same type as value. |
Returns: BOOL. NULL if either argument is NULL.
Examples
Underscores are literal in STARTS_WITH
In LIKE the _ matches the x; it has to be escaped to mean an underscore.
SELECT
STARTS_WITH('tmpx_orders', 'tmp_') AS starts_with_result,
'tmpx_orders' LIKE 'tmp_%' AS like_result,
'tmpx_orders' LIKE r'tmp\_%' AS escaped_like_result;| starts_with_result | like_result | escaped_like_result |
|---|---|---|
| false | true | false |
Case sensitivity and edge cases
SELECT
STARTS_WITH('Order-1001', 'order') AS case_sensitive,
STARTS_WITH(LOWER('Order-1001'), 'order') AS lowercased,
STARTS_WITH('Order-1001', '') AS empty_prefix,
STARTS_WITH(CAST(NULL AS STRING), 'O') AS null_value;| case_sensitive | lowercased | empty_prefix | null_value |
|---|---|---|---|
| false | true | true | NULL |
Find staging tables
WITH tables AS (
SELECT 'stg_orders' AS table_name UNION ALL
SELECT 'stgx_test' UNION ALL
SELECT 'orders'
)
SELECT table_name
FROM tables
WHERE STARTS_WITH(table_name, 'stg_')
ORDER BY table_name;| table_name |
|---|
| stg_orders |
Common mistakes
- Using
LIKE 'prefix_%'for prefixes that contain_or%. The underscore is a wildcard; use STARTS_WITH or escape it asr'prefix\_%'. - Porting Snowflake's STARTSWITH. BigQuery spells it STARTS_WITH, with an underscore;
STARTSWITHfails with Function not found. - Expecting case-insensitive matching. Compare
STARTS_WITH(LOWER(value), 'abc')or useREGEXP_CONTAINS(value, r'(?i)^abc').
Cost and performance
- On a table with a search index, BigQuery can use the index for
STARTS_WITH(column, 'prefix')andcolumn LIKE 'prefix%', but not when the column is wrapped in another function such as SUBSTR.
FAQ
What is the difference between STARTS_WITH and LIKE in BigQuery?
STARTS_WITH(col, 'abc') and col LIKE 'abc%' usually return the same result. STARTS_WITH treats _ and % literally, while LIKE treats them as wildcards unless you escape them.
Is STARTS_WITH case-sensitive?
Yes. Lowercase both sides for a case-insensitive prefix check.
How do I check whether a string starts with one of several prefixes?
Combine with OR, or use a quantified LIKE: col LIKE ANY ('stg\\_%', 'tmp\\_%').
Related
More string functions
All BigQuery string functions · All functions · BigQuery error messages
