BigQuery · Query syntax
BigQuery SELECT * REPLACE
SELECT * REPLACE (expression AS column) returns every column but swaps the value of the named columns for an expression, keeping their names and positions. Use it to mask, round, cast or clean a few columns without listing all the others.
SELECT * REPLACE (expression AS column_name, ...) expands the star and, for each listed column, outputs the expression instead of the stored value. The column keeps its name and its position in the result, so downstream code that relies on the column order, a UNION ALL or an INSERT ... SELECT * keeps working. The expression can change the type: CAST(amount AS STRING) AS amount returns a STRING column called amount.
Every name in the REPLACE list must exist in the star expansion. You can combine it with SELECT * EXCEPT to drop some columns and rewrite others in one clause; EXCEPT comes first. Like EXCEPT, it works on a qualified star (o.* REPLACE (...)). PostgreSQL and MySQL have no equivalent; Snowflake supports SELECT * REPLACE with the same meaning.
Typical uses: masking personal data in a view ('***' AS email), normalising values (LOWER(country) AS country), fixing types before a union, and rebuilding a table with a corrected column via CREATE OR REPLACE TABLE AS SELECT. It does not reduce bytes scanned: the replaced column is usually still read, because the expression typically uses it.
Syntax
SELECT [expression.]* [EXCEPT (column_name[, ...])] REPLACE (expression AS column_name[, ...])
FROM ...Arguments and return value
| Argument | Description |
|---|---|
expression | Any expression, often based on the original column. Its type becomes the column's type. |
column_name | An existing column of the star expansion. Unqualified: use o.* REPLACE (...) for one table of a join. |
Returns: All columns of the star expansion, in their original order, with the listed columns replaced by the expressions.
Examples
Mask and normalise columns
WITH customers AS (
SELECT 7 AS customer_id, 'Ana.Silva@Example.com' AS email, 'de' AS country
)
SELECT * REPLACE (
CONCAT(LEFT(email, 1), '***@', SPLIT(email, '@')[OFFSET(1)]) AS email,
UPPER(country) AS country
)
FROM customers;| customer_id | country | |
|---|---|---|
| 7 | A***@Example.com | DE |
Change a column's type, keeping its position
amount becomes a rounded NUMERIC in the same position, so the result can be unioned with a table that stores NUMERIC.
WITH payments AS (
SELECT 'p_1' AS payment_id, 19.994 AS amount, 'EUR' AS currency UNION ALL
SELECT 'p_2', 5.5, 'EUR'
)
SELECT * REPLACE (ROUND(CAST(amount AS NUMERIC), 2) AS amount)
FROM payments
ORDER BY payment_id;| payment_id | amount | currency |
|---|---|---|
| p_1 | 19.99 | EUR |
| p_2 | 5.5 | EUR |
EXCEPT and REPLACE together in a masking view
Drops the password hash and masks the email for analysts, without listing the remaining columns.
CREATE OR REPLACE VIEW `project.reporting.users_masked` AS
SELECT * EXCEPT (password_hash)
REPLACE (TO_HEX(SHA256(email)) AS email)
FROM `project.dataset.users`;Common mistakes
- Using REPLACE to add a column. It only rewrites existing columns; a name that is not in the star expansion fails. Add new columns after the star:
SELECT *, expr AS new_col. - Putting EXCEPT after REPLACE. The order is
* EXCEPT (...) REPLACE (...). - Writing the alias first as in an UPDATE (
REPLACE (email = '***')). The form isexpression AS column_name. - Relying on REPLACE in a view for security alone when users can query the base table. Grant access to the view only, as an authorized view.
Cost and performance
- REPLACE does not change what is scanned: the columns used in the expressions are read and billed as usual. To save bytes, drop columns with EXCEPT.
FAQ
How do I modify one column while selecting all columns in BigQuery?
SELECT * REPLACE (UPPER(country) AS country) FROM t. The column keeps its name and position.
What is the difference between SELECT EXCEPT and SELECT REPLACE?
EXCEPT removes columns from the result; REPLACE keeps them but substitutes a new value. You can use both in one select list, EXCEPT first.
Related
More query syntax
All BigQuery query syntax · All statements and clauses · BigQuery error messages
