无法在 postgres 中将列更改为整数或布尔值
Not able to alter column to integer or boolean in postgres
我尝试使用以下查询 postgres 将列更改为整数类型。
alter table pm_user alter column testing2 type integer using testing2::integer;
pm_user - table 姓名
testing2 - 列名
上面的列可以是任何类型的数据,例如:boolean、text、varchar(256)。
它给出错误 'ERROR: invalid input syntax for integer: "NULL"' 我根据之前在此网站上提出的查询尝试了以下解决方案,但它对我不起作用。
alter table pm_user alter column testing2 type integer using (testing2::integer);
alter table pm_user alter column testing2 type integer;
alter table pm_user alter column testing2 type numeric using (testing2::numeric);
alter table pm_user alter column testing2 type numeric (10,2);
实际问题是什么?在哪里
点输入为空?哪一个被认为是空的?我可以提供什么解决方案。当我尝试更改为
时,相同的查询有效
alter table pm_user alter column testing2 type text using testing2::text;
alter table pm_user alter column testing2 type varchar(256)using testing2::varchar(256);
它也不适用于布尔值。
错误消息表明您的列包含字符串文字 'NULL'
而不是 "real" 空值。像这样:
create table pm_user (testing2 varchar);
insert into pm_user (testing2) values ('NULL');
(注意 'NULL'
- 这与 NULL
不同)
因此,将字符串转换为数字的表达式必须处理好这一点。
以下将把 varchar 列转换为整数。该列中不是正确整数的任何值都将更改为空值:
alter table pm_user alter column testing2 type integer
using case when testing2 ~ '^[-+0-9]+$' then testing2::integer else null end;
~
是 Postgres 的正则表达式运算符。 testing2 ~ '^[0-9]+$'
测试该列是否仅包含数字。
我尝试使用以下查询 postgres 将列更改为整数类型。
alter table pm_user alter column testing2 type integer using testing2::integer;
pm_user - table 姓名
testing2 - 列名
上面的列可以是任何类型的数据,例如:boolean、text、varchar(256)。
它给出错误 'ERROR: invalid input syntax for integer: "NULL"' 我根据之前在此网站上提出的查询尝试了以下解决方案,但它对我不起作用。
alter table pm_user alter column testing2 type integer using (testing2::integer);
alter table pm_user alter column testing2 type integer;
alter table pm_user alter column testing2 type numeric using (testing2::numeric);
alter table pm_user alter column testing2 type numeric (10,2);
实际问题是什么?在哪里 点输入为空?哪一个被认为是空的?我可以提供什么解决方案。当我尝试更改为
时,相同的查询有效alter table pm_user alter column testing2 type text using testing2::text;
alter table pm_user alter column testing2 type varchar(256)using testing2::varchar(256);
它也不适用于布尔值。
错误消息表明您的列包含字符串文字 'NULL'
而不是 "real" 空值。像这样:
create table pm_user (testing2 varchar);
insert into pm_user (testing2) values ('NULL');
(注意 'NULL'
- 这与 NULL
不同)
因此,将字符串转换为数字的表达式必须处理好这一点。
以下将把 varchar 列转换为整数。该列中不是正确整数的任何值都将更改为空值:
alter table pm_user alter column testing2 type integer
using case when testing2 ~ '^[-+0-9]+$' then testing2::integer else null end;
~
是 Postgres 的正则表达式运算符。 testing2 ~ '^[0-9]+$'
测试该列是否仅包含数字。