如何在雪花中将列的数据类型从布尔值更改为varchar(50)
?
OTCONTROLLABLE BOOLEAN
在以下示例中为OTCONTROLLABLE varchar(50)
create or replace TABLE ORIGINATIONUNRECOGNIZEDREVENUE_OTCN030 (
YEAR VARCHAR(50),
MONTH VARCHAR(50),
OTCONTROLLABLE BOOLEAN);
答案 0 :(得分:6)
Neeraj-您可以运行CTAS语句来创建新表,然后交换表。
可在此处浏览CTAS的文档;
https://docs.snowflake.net/manuals/sql-reference/sql/create-table.html#variant-syntax
交换语句;
https://docs.snowflake.net/manuals/sql-reference/sql/alter-table.html#syntax
答案 1 :(得分:2)
我会推荐@bstora建议的方法,但这也是可供参考的另一种选择:
varchar
列boolean
语句将varchar
列中的值复制到新的update
列中boolean
列boolean
列具有相同的名称-- Setup example table
create or replace temporary table public.example_table (
bool_col boolean
);
-- Insert some sample values
insert overwrite into public.example_table values (1), (0);
-- Create a new varchar column to hold the boolean values as varchar
alter table public.example_table add column temp_varchar_col varchar(50);
-- Copy all of the values from the boolean column to the new varchar column
update public.example_table set temp_varchar_col = bool_col;
-- Check the data looks okay in the new column
select * from public.example_table;
-- Drop the old boolean-typed column
alter table public.example_table drop column bool_col;
-- Rename the varchar column to the same name as the dropped boolean-typed column
alter table public.example_table rename column temp_varchar_col to bool_col;
-- Check everything looks okay
select * from public.example_table;
bstora提到的交换表方法的优点是用户永远不会看到有两列的表,而且它可能也更快。