如何在PostgreSQL 8.4中将列数据类型从字符更改为数字 [英] How to change column datatype from character to numeric in PostgreSQL 8.4

查看:148
本文介绍了如何在PostgreSQL 8.4中将列数据类型从字符更改为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下查询:

ALTER TABLE presales ALTER COLUMN code TYPE numeric(10,0); 

更改列的数据类型为字符(20) numeric(10,0),但出现错误:

to change the datatype of a column from character(20) to numeric(10,0) but I am getting the error:


列代码不能转换为数字类型

column "code" cannot be cast to type numeric


推荐答案

您可以尝试使用 使用中

You can try using USING:


可选的 USING 子句指定如何从旧的计算新的列值;如果省略,则默认转换与从旧数据类型到新数据类型的分配相同。如果没有从旧类型到新类型的隐式或赋值转换,则必须提供 USING 子句。

The optional 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 USING clause must be provided if there is no implicit or assignment cast from old to new type.

因此这可能有效(取决于您的数据):

So this might work (depending on your data):

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);

如果您在代码中有任何东西不能转换为数字;如果使用失败,则必须手动清理非数字数据,然后再更改列类型。

This will fail if you have anything in code that cannot be cast to numeric; if the USING fails, you'll have to clean up the non-numeric data by hand before changing the column type.

这篇关于如何在PostgreSQL 8.4中将列数据类型从字符更改为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆