BigQuery · String function
BigQuery REPLACE
REPLACE(value, from, to) replaces every occurrence of a substring: REPLACE(phone, ' ', '') removes all spaces. Matching is exact and case-sensitive; use REGEXP_REPLACE for patterns and TRANSLATE for single characters.
REPLACE substitutes every non-overlapping occurrence of from_pattern with to_pattern, scanning left to right. Pass an empty string as to_pattern to remove text. The match is literal: no wildcards, no regular expressions, and no case folding, so REPLACE(status, 'paid', ...) doesn't touch Paid or PAID.
An empty from_pattern returns the value unchanged. A NULL in any argument returns NULL, including a NULL replacement.
To remove several different characters in one call, use TRANSLATE or REGEXP_REPLACE with a character class (r'[()\- ]') rather than nesting REPLACE calls. For case-insensitive replacement, use REGEXP_REPLACE with the (?i) flag.
Syntax
REPLACE(original_value, from_pattern, to_pattern)Arguments and return value
| Argument | Description |
|---|---|
original_value | The STRING or BYTES value to change. |
from_pattern | The literal text to find. If empty, nothing is replaced. |
to_pattern | The replacement text. Use '' to delete matches. |
Returns: STRING or BYTES, the same type as original_value.
Examples
Remove and replace text
SELECT
REPLACE('+1 (415) 555-0100', ' ', '') AS without_spaces,
REPLACE('2026/11/23', '/', '-') AS iso_like_date,
REPLACE('Paid paid PAID', 'paid', 'settled') AS case_sensitive;| without_spaces | iso_like_date | case_sensitive |
|---|---|---|
| +1(415)555-0100 | 2026-11-23 | Paid settled PAID |
Non-overlapping matches, empty pattern and NULLs
SELECT
REPLACE('banana', 'ana', 'X') AS non_overlapping,
REPLACE('banana', '', 'X') AS empty_pattern,
REPLACE('banana', 'a', CAST(NULL AS STRING)) AS null_replacement;| non_overlapping | empty_pattern | null_replacement |
|---|---|---|
| bXna | banana | NULL |
Clean a currency string before casting
WITH invoices AS (
SELECT 'INV-1' AS invoice_id, '$1,234.50' AS amount_text UNION ALL
SELECT 'INV-2', '$980.00'
)
SELECT
invoice_id,
CAST(REPLACE(REPLACE(amount_text, '$', ''), ',', '') AS NUMERIC) AS amount
FROM invoices
ORDER BY invoice_id;| invoice_id | amount |
|---|---|
| INV-1 | 1234.5 |
| INV-2 | 980 |
Common mistakes
- Expecting case-insensitive matching. REPLACE is case-sensitive. Use
REGEXP_REPLACE(value, r'(?i)paid', 'settled'). - Expecting wildcards.
%and_are literal characters in REPLACE. Use REGEXP_REPLACE for patterns. - Calling REPLACE with two arguments, as in Snowflake or Redshift, to delete text. BigQuery requires three; pass
''as the replacement. - Nesting many REPLACE calls to strip characters.
TRANSLATE(phone, '()- ', '')orREGEXP_REPLACE(phone, r'[^0-9+]', '')is shorter and easier to maintain.
FAQ
How do I remove characters from a string in BigQuery?
For one substring, REPLACE(value, 'x', ''). For a set of characters, TRANSLATE(value, '()- ', ''). For everything except allowed characters, REGEXP_REPLACE(value, r'[^0-9]', '').
Is BigQuery REPLACE case-sensitive?
Yes. To replace regardless of case, use REGEXP_REPLACE(value, r'(?i)text', 'replacement').
What is the difference between REPLACE and REGEXP_REPLACE?
REPLACE matches literal text and is simpler and cheaper. REGEXP_REPLACE matches an RE2 regular expression and can reuse captured groups in the replacement with \1.
Related
More string functions
All BigQuery string functions · All functions · BigQuery error messages
