BigQuery · String function
BigQuery TRIM
TRIM(value[, characters]) removes leading and trailing whitespace from a string, or every leading and trailing character found in the characters set: TRIM('xxapplexx', 'x') returns apple. LTRIM and RTRIM trim one side only.
Without a second argument, TRIM removes whitespace from both ends of a STRING. That includes spaces, tabs and line breaks, and also Unicode spaces such as the non-breaking space (U+00A0) that often arrives in copy-pasted or scraped data. Whitespace inside the string is kept.
With a second argument, TRIM removes characters from both ends as long as they belong to that set. The second argument is a set of characters, not a substring: every leading or trailing character that appears anywhere in the set is removed, in any order. So TRIM(file_name, '.csv') is not "remove the .csv extension": it also eats any c, s, v or . at the end of the name. To remove an exact prefix or suffix, use REGEXP_REPLACE with an anchored pattern.
For BYTES values the set is required. BigQuery doesn't support the standard TRIM(BOTH 'x' FROM value) syntax; use TRIM(value, 'x'), LTRIM for LEADING and RTRIM for TRAILING.
Syntax
TRIM(value_to_trim[, set_of_characters_to_remove])Arguments and return value
| Argument | Description |
|---|---|
value_to_trim | The STRING or BYTES value to trim. |
set_of_characters_to_remove | Optional set of characters (STRING) or bytes (BYTES) to remove from both ends. Defaults to whitespace for STRING; required for BYTES. |
Returns: STRING or BYTES, the same type as value_to_trim.
Examples
Whitespace, tabs and non-breaking spaces
SELECT
CONCAT('[', TRIM(' order 42 '), ']') AS spaces,
CONCAT('[', TRIM('\t order 42 \n'), ']') AS tab_and_newline,
CONCAT('[', TRIM('\u00a0order 42\u00a0'), ']') AS non_breaking_space;| spaces | tab_and_newline | non_breaking_space |
|---|---|---|
| [order 42] | [order 42] | [order 42] |
The second argument is a set of characters
trims_too_much loses the final cs of metrics because c and s are in the set.
SELECT
TRIM('**SALE**', '*') AS stars,
TRIM('-_-order_id-_-', '-_') AS mixed_set,
TRIM('metrics.csv', '.csv') AS trims_too_much,
REGEXP_REPLACE('metrics.csv', r'\.csv$', '') AS exact_suffix;| stars | mixed_set | trims_too_much | exact_suffix |
|---|---|---|---|
| SALE | order_id | metri | metrics |
Clean keys before a join
WITH crm AS (
SELECT ' ACME-001 ' AS account_code, 'ACME GmbH' AS account_name
),
billing AS (
SELECT 'ACME-001' AS account_code, 1250 AS mrr
)
SELECT c.account_name, b.mrr
FROM crm AS c
JOIN billing AS b ON TRIM(c.account_code) = b.account_code;| account_name | mrr |
|---|---|
| ACME GmbH | 1250 |
Common mistakes
- The second argument is a set of characters, not a substring: every leading or trailing character that appears anywhere in the set is removed, in any order.
TRIM('metrics.csv', '.csv')returnsmetri. UseREGEXP_REPLACE(value, r'\.csv$', '')to remove an exact suffix. - Using the SQL standard syntax.
TRIM(BOTH 'x' FROM value),TRIM(LEADING ...)andTRIM(TRAILING ...)are syntax errors in BigQuery; useTRIM(value, 'x'), LTRIM and RTRIM. - Expecting TRIM to collapse inner spaces.
TRIM('a b')keeps the three spaces; useREGEXP_REPLACE(value, r'\s+', ' '). - Calling TRIM on BYTES without a set. It fails with No matching signature; BYTES need an explicit set such as
TRIM(b, b' ').
Cost and performance
- If you trim the same columns in many queries, store the trimmed values once. Google's compute best practices recommend materializing string cleanup instead of repeating it, and a function on a clustered filter column also stops block pruning.
FAQ
How do I remove spaces from the start and end of a string in BigQuery?
Use TRIM(value). It also removes tabs, newlines and Unicode spaces such as the non-breaking space. Use LTRIM or RTRIM for one side.
How do I remove all spaces from a string?
TRIM only touches the ends. Use REPLACE(value, ' ', '') for spaces or REGEXP_REPLACE(value, r'\s', '') for every whitespace character.
How do I remove a specific character from both ends?
Pass it as the second argument: TRIM(value, '"') removes leading and trailing double quotes. Several characters can go in the set, for example TRIM(value, '" ') removes double quotes and spaces.
Related
Errors you may hit
More string functions
All BigQuery string functions · All functions · BigQuery error messages
