Request a demo

BigQuery · String function

BigQuery RIGHT

RIGHT(value, length) returns the last length characters of a string (or bytes of a BYTES value): RIGHT('4111111111111111', 4) returns 1111. Shorter values are returned whole; a negative length is an error.

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

RIGHT is the mirror of LEFT and a readable shortcut for SUBSTR(value, -length). It counts characters for STRING and bytes for BYTES. A length of 0 returns an empty string, a length longer than the value returns the whole value, and NULL in either argument returns NULL.

A negative length raises Second argument in RIGHT() cannot be negative, which typically happens when the length is computed from a position function on a row where the delimiter is missing.

Typical uses are masking (show the last four digits of an account number), reading file extensions or fixed-width suffixes, and checking check digits. To test whether a string ends with a value, ENDS_WITH is clearer than comparing RIGHT to a literal.

Syntax

RIGHT(value, length)

Arguments and return value

ArgumentDescription
valueThe STRING or BYTES value.
lengthINT64 number of characters (STRING) or bytes (BYTES) to return. Must not be negative.

Returns: STRING or BYTES, the same type as value.

Examples

Suffixes and short values

SELECT
  RIGHT('4111111111111111', 4) AS last_four,
  RIGHT('orders_2026_11', 2) AS month_suffix,
  RIGHT('EU', 5) AS shorter_than_length,
  RIGHT('Zürich', 3) AS characters_not_bytes;
Result
last_fourmonth_suffixshorter_than_lengthcharacters_not_bytes
111111EUich

Mask an account number

WITH accounts AS (
  SELECT 'ACC-1' AS account_id, 'DE89370400440532013000' AS iban UNION ALL
  SELECT 'ACC-2', 'FR1420041010050500013M02606'
)
SELECT
  account_id,
  CONCAT(REPEAT('*', LENGTH(iban) - 4), RIGHT(iban, 4)) AS masked_iban
FROM accounts
ORDER BY account_id;
Result
account_idmasked_iban
ACC-1******************3000
ACC-2***********************2606

Common mistakes

  • Computing a negative length. RIGHT(path, LENGTH(path) - INSTR(path, '/', -1)) is fine, but lengths derived from STRPOS(...) - 1 turn negative when the delimiter is missing and fail the query.
  • Calling RIGHT on a number. RIGHT(order_id, 2) fails with No matching signature when order_id is INT64; use RIGHT(CAST(order_id AS STRING), 2) or MOD(order_id, 100).
  • Comparing RIGHT with a literal to test a suffix. RIGHT(file, 4) = '.csv' works, but ENDS_WITH(file, '.csv') doesn't need the length and can use a search index.

FAQ

How do I get the last N characters of a string in BigQuery?

Use RIGHT(value, n) or SUBSTR(value, -n). Both return the full string when it has fewer than n characters.

How do I remove the last N characters of a string?

Use LEFT(value, LENGTH(value) - n), guarded with GREATEST(LENGTH(value) - n, 0) if some values can be shorter than n.

Does RIGHT work on BYTES?

Yes. For BYTES, length is a number of bytes and the result is BYTES.

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.