我使用以下查询:
ALTER TABLE presales ALTER COLUMN code TYPE numeric(10,0);
将列的数据类型从字符(20)更改为数字(10,0),但我得到错误:
column “code” cannot be cast to type numeric
您可以尝试使用
USING
:
The optional @H_301_15@USING clause specifies how to compute the new column value from the old; if omitted,the default conversion is the same as an assignment cast from old data type to new. A @H_301_15@USING clause must be provided if there is no implicit or assignment cast from old to new type.
所以这可能工作(取决于您的数据):
alter table presales alter column code type numeric(10,0) using code::numeric; -- Or if you prefer standard casting... alter table presales alter column code type numeric(10,0) using cast(code as numeric);
这将失败,如果你有任何代码,不能转换为数字;如果USING失败,您必须在更改列类型之前手动清理非数字数据。