Request a demo

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.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

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

ArgumentDescription
original_valueThe STRING or BYTES value to pad.
return_lengthINT64 length of the result in characters (STRING) or bytes (BYTES). Must not be negative.
patternOptional 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;
Result
store_idstore_code
700007
125001250

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;
Result
truncatedrepeated_patterndefault_spaces
1234ababa7" 7"

Negative numbers: LPAD vs FORMAT

SELECT
  LPAD('-42', 6, '0') AS with_lpad,
  FORMAT('%06d', -42) AS with_format;
Result
with_lpadwith_format
000-42-00042

Common mistakes

  • Assuming LPAD never shortens values. A value longer than return_length is truncated to its leftmost characters, which can silently corrupt IDs. Check LENGTH(value) <= n or use a length large enough for all values.
  • Zero-padding signed numbers with LPAD. LPAD('-42', 6, '0') returns 000-42; use FORMAT('%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 with Third 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.

Errors you may hit

More string functions

All BigQuery string functions · All functions · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.