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.
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
| Argument | Description |
|---|---|
value | The STRING or BYTES value. |
length | INT64 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;| last_four | month_suffix | shorter_than_length | characters_not_bytes |
|---|---|---|---|
| 1111 | 11 | EU | ich |
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;| account_id | masked_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 fromSTRPOS(...) - 1turn negative when the delimiter is missing and fail the query. - Calling RIGHT on a number.
RIGHT(order_id, 2)fails with No matching signature whenorder_idis INT64; useRIGHT(CAST(order_id AS STRING), 2)orMOD(order_id, 100). - Comparing RIGHT with a literal to test a suffix.
RIGHT(file, 4) = '.csv'works, butENDS_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.
Related
Errors you may hit
More string functions
All BigQuery string functions · All functions · BigQuery error messages
