加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在PostgreSQL 8.4中将字段数据类型从字符更改为数字

发布时间:2020-12-13 16:53:34 所属栏目:百科 来源:网络整理
导读:我使用以下查询: ALTER TABLE presales ALTER COLUMN code TYPE numeric(10,0); 将列的数据类型从字符(20)更改为数字(10,0),但我得到错误: column “code” cannot be cast to type numeric 您可以尝试使用 USING : The optional USING clause specifies
我使用以下查询:
ALTER TABLE presales ALTER COLUMN code TYPE numeric(10,0);

将列的数据类型从字符(20)更改为数字(10,0),但我得到错误:

column “code” cannot be cast to type numeric

您可以尝试使用 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.

所以这可能工作(取决于您的数据):

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失败,您必须在更改列类型之前手动清理非数字数据。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读