错误:无法替换PostgreSQL中的文本字符串

时间:2017-09-02 22:54:41

标签: sql postgresql

在我的PostgreSQL 9.5数据库中,我有一个名为my_table的表,其中包含三列id character varying (254)val_1 numericval_2 numeric。以下查询:

Select id, val_1, val_2 from my_table limit 4;

返回四个样本行:

id         val_1   val_2
1147_1     72      61
1228_1     73      67
1255_1     74      68
1337_1     79      66

我想更改id列的数据类型,因此我尝试了:

Alter table my_table alter column id type integer using id::integer;

但是我收到了这个错误:

ERROR:  invalid input syntax for integer: "1147_1"

为了摆脱这些"",我尝试了:

Update my_table set id = replace(id, '"','');

查询已成功返回。但是,当我尝试更改数据类型时,我再次得到相同的错误ERROR: invalid input syntax for integer: "1147_1"。有人可以帮我解决这个问题吗?即使我成功执行了replace()命令,我也无法弄清楚为什么我会一直得到同样的错误。

1 个答案:

答案 0 :(得分:2)

您必须将id值更改为有效整数,例如删除下划线:

alter table my_table alter column id type integer using replace(id, '_', '')::integer;