BigQuery · String function
BigQuery LPAD
LPAD(value, length[, pattern]) pads a string on the left to a fixed length: LPAD('42', 6, '0') returns 000042. The default pattern is a space. If the value is already longer, LPAD truncates it to length.
LPAD prepends copies of pattern until the result is return_length characters long (bytes for BYTES). A multi-character pattern is repeated and cut off where the length is reached. The default pattern is a single space.
Two behaviours surprise people. First, when the value is longer than return_length, LPAD doesn't leave it alone: it truncates it, keeping the leftmost characters, so LPAD('1234567', 4, '0') returns 1234. Second, LPAD knows nothing about numbers: padding -42 with zeros puts the zeros before the minus sign. For numeric zero-padding, FORMAT('%06d', n) handles signs correctly.
The value must be a STRING or BYTES, so cast numbers first: LPAD(CAST(store_id AS STRING), 4, '0'). An empty pattern and a negative length are errors; NULL in any argument returns NULL.
Syntax
LPAD(original_value, return_length[, pattern])Arguments and return value
| Argument | Description |
|---|---|
original_value | The STRING or BYTES value to pad. |
return_length | INT64 length of the result in characters (STRING) or bytes (BYTES). Must not be negative. |
pattern | Optional padding text, the same type as original_value. Defaults to a space. Must not be empty. |
Returns: STRING or BYTES, the same type as original_value.
Examples
Zero-pad an ID
WITH stores AS (
SELECT 7 AS store_id UNION ALL
SELECT 1250
)
SELECT store_id, LPAD(CAST(store_id AS STRING), 5, '0') AS store_code
FROM stores
ORDER BY store_id;| store_id | store_code |
|---|---|
| 7 | 00007 |
| 1250 | 01250 |
Truncation, repeated patterns and default spaces
FORMAT with %T shows the result as a quoted literal so the leading spaces are visible.
SELECT
LPAD('1234567', 4, '0') AS truncated,
LPAD('7', 6, 'ab') AS repeated_pattern,
FORMAT('%T', LPAD('7', 3)) AS default_spaces;| truncated | repeated_pattern | default_spaces |
|---|---|---|
| 1234 | ababa7 | " 7" |
Negative numbers: LPAD vs FORMAT
SELECT
LPAD('-42', 6, '0') AS with_lpad,
FORMAT('%06d', -42) AS with_format;| with_lpad | with_format |
|---|---|
| 000-42 | -00042 |
Common mistakes
- Assuming LPAD never shortens values. A value longer than
return_lengthis truncated to its leftmost characters, which can silently corrupt IDs. CheckLENGTH(value) <= nor use a length large enough for all values. - Zero-padding signed numbers with LPAD.
LPAD('-42', 6, '0')returns000-42; useFORMAT('%06d', -42). - Passing a number.
LPAD(42, 6, '0')fails with No matching signature; cast to STRING first. - Passing an empty pattern, for example from a NULL-replaced column.
LPAD(value, 5, '')fails withThird argument (pad pattern) for LPAD/RPAD cannot be empty.
FAQ
How do I add leading zeros in BigQuery?
Use LPAD(CAST(n AS STRING), width, '0') for non-negative integers, or FORMAT('%0*d', width, n) / FORMAT('%06d', n), which also handles negative numbers.
What happens if the string is longer than the target length?
LPAD truncates it to return_length characters, keeping the start of the string.
What is the difference between LPAD and RPAD?
LPAD adds the pattern before the value (right-aligns it); RPAD adds it after the value (left-aligns it). Both truncate values that are too long.
Related
Errors you may hit
More string functions
All BigQuery string functions · All functions · BigQuery error messages
