Coerce a column's data type
To change a column's data type into a coercible type, use the ALTER COLUMN SET DATA TYPE DDL statement. The following example creates a table with a column of type INT64, then updates the type to NUMERIC:
Syntax:
CREATE TABLE mydataset.mytable(c1 INT64);
Alter syntax:
ALTER TABLE mydataset.mytable
ALTER COLUMN c1 SET DATA TYPE NUMERIC;
Cast a column's data type
To change a column's data type into a castable type, use a SQL query to select the table data, cast the relevant column, and overwrite the table. Casting and overwriting is not recommended for very large tables because it requires a full table scan.
The following example shows a SQL query that selects all the data from column_two and column_three in mydataset.mytable and casts column_one from DATE to STRING. The query result is used to overwrite the existing table. The overwritten table stores column_one as a STRING data type.
Go to BigQuery
In the Query editor, enter the following query to select all of the data from column_two and column_three in mydataset.mytable and to cast column_one from DATE to STRING. The query uses an alias to cast column_one with the same name. mydataset.mytable is in your default project.
SELECT
column_two,
column_three,
CAST(column_one AS STRING) AS column_one
FROM
mydataset.mytable;
4.Click More and select Query settings.
5.In the Destination section, do the following:
6.Select Set a destination table for query results.
7.For Project name, leave the value set to your default project. This is the
project that contains mydataset.mytable.
8.For Dataset, choose dataset.
9.In the Table Id field, enter mytable.
10.For Destination table write preference, select Overwrite table. This option overwrites mytable using the query results.
11.Optionally, choose your data's location.
12.To update the settings, click Save.
13.Click Run.
- Refresh, When the query job completes, the data type of column_one is STRING.
For more info: https://cloud.google.com/bigquery/docs/managing-table-schemas#console_3