BigQuery · String function
BigQuery LTRIM
LTRIM(value[, characters]) removes leading whitespace, or every leading character found in the characters set: LTRIM('000123', '0') returns 123. It works like TRIM but only on the start of the string.
LTRIM is TRIM restricted to the left side. Without a second argument it removes leading whitespace, including tabs, newlines and Unicode spaces such as U+00A0. With a second argument it removes leading characters for 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.
A common use is stripping leading zeros from codes. Watch the edge case: a value made only of zeros becomes an empty string, not 0. If the code is numeric, CAST(code AS INT64) is often the better tool.
For BYTES values the set is required. LTRIM replaces TRIM(LEADING ... FROM ...), which BigQuery doesn't support.
Syntax
LTRIM(value1[, value2])Arguments and return value
| Argument | Description |
|---|---|
value1 | The STRING or BYTES value to trim. |
value2 | Optional set of characters (STRING) or bytes (BYTES) to remove from the start. Defaults to whitespace for STRING; required for BYTES. |
Returns: STRING or BYTES, the same type as value1.
Examples
Leading whitespace and leading zeros
Trailing spaces are kept. A value of only zeros becomes an empty string.
SELECT
CONCAT('[', LTRIM(' order 42 '), ']') AS leading_spaces,
LTRIM('000123', '0') AS leading_zeros,
LTRIM('0000', '0') AS only_zeros;| leading_spaces | leading_zeros | only_zeros |
|---|---|---|
| [order 42 ] | 123 |
Strip leading zeros but keep a single zero
WITH codes AS (
SELECT '00420' AS store_code UNION ALL
SELECT '0000'
)
SELECT store_code, IF(LTRIM(store_code, '0') = '', '0', LTRIM(store_code, '0')) AS cleaned
FROM codes
ORDER BY store_code;| store_code | cleaned |
|---|---|
| 0000 | 0 |
| 00420 | 420 |
Remove a leading prefix character set
SELECT LTRIM('#/#/orders', '#/') AS path;| path |
|---|
| orders |
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.
LTRIM('stg_sales', 'stg_')returnsalesbecausesis in the set. UseREGEXP_REPLACE(value, r'^stg_', '')to remove an exact prefix. - Turning
'000'into an empty string when stripping zeros. Handle it withIF(... = '', '0', ...)or cast to INT64 instead. - Using
TRIM(LEADING '0' FROM value). That standard syntax fails in BigQuery; writeLTRIM(value, '0').
FAQ
How do I remove leading zeros in BigQuery?
LTRIM(value, '0'), keeping in mind that all-zero values become ''. If the value is a number stored as text, CAST(value AS INT64) removes the zeros and validates the input.
How do I remove a prefix from a string in BigQuery?
LTRIM removes a set of characters, not a word. For an exact prefix use REGEXP_REPLACE(value, r'^prefix', ''), or IF(STARTS_WITH(value, 'prefix'), SUBSTR(value, LENGTH('prefix') + 1), value).
Related
More string functions
All BigQuery string functions · All functions · BigQuery error messages
