无法更改 Big Query 中的列数据类型
Unable to alter column data type in Big Query
我们将数据库导入 BigQuery,但很多列的数据类型不正确,其中许多列存储为 STRING。我想通过更改 BigQuery
中的数据类型来修复它们
ALTER TABLE my.data_set.my_table ALTER COLUMN create_date SET DATA TYPE DATE;
但是我得到了
ALTER TABLE ALTER COLUMN SET DATA TYPE requires that the existing column type (STRING) is assignable to the new type (DATE)
如何解决?
不幸的是,据我所知,无法使用 ALTER TABLE
将数据类型从 STRING
转换为 DATE
,只能使用您想要的模式重新创建它。
CREATE OR REPLACE TABLE testset.tbl AS
SELECT 'a' AS col1, '2022-05-16' AS create_date
UNION ALL
SELECT 'a' AS col1, '2022-05-14' AS create_date
;
-- ALTER TABLE testset.tbl ALTER COLUMN create_date SET DATA TYPE DATE;
-- Query error: ALTER TABLE ALTER COLUMN SET DATA TYPE requires that
-- the existing column type (STRING) is assignable to the new type (DATE) at [7:25]
-- Create it again.
CREATE OR REPLACE TABLE testset.tbl AS
SELECT * REPLACE(SAFE_CAST(create_date AS DATE) AS create_date)
FROM testset.tbl;
我们将数据库导入 BigQuery,但很多列的数据类型不正确,其中许多列存储为 STRING。我想通过更改 BigQuery
中的数据类型来修复它们ALTER TABLE my.data_set.my_table ALTER COLUMN create_date SET DATA TYPE DATE;
但是我得到了
ALTER TABLE ALTER COLUMN SET DATA TYPE requires that the existing column type (STRING) is assignable to the new type (DATE)
如何解决?
不幸的是,据我所知,无法使用 ALTER TABLE
将数据类型从 STRING
转换为 DATE
,只能使用您想要的模式重新创建它。
CREATE OR REPLACE TABLE testset.tbl AS
SELECT 'a' AS col1, '2022-05-16' AS create_date
UNION ALL
SELECT 'a' AS col1, '2022-05-14' AS create_date
;
-- ALTER TABLE testset.tbl ALTER COLUMN create_date SET DATA TYPE DATE;
-- Query error: ALTER TABLE ALTER COLUMN SET DATA TYPE requires that
-- the existing column type (STRING) is assignable to the new type (DATE) at [7:25]
-- Create it again.
CREATE OR REPLACE TABLE testset.tbl AS
SELECT * REPLACE(SAFE_CAST(create_date AS DATE) AS create_date)
FROM testset.tbl;