0
如用于列(xx)的列(xx)其中数据类型为varchar并且其中包含值,例如'cons','alts'以将其转换以int ... 如果不可能这样做我们有什么其他选项是否有可能在列中已经有数据时更改列的数据类型
如用于列(xx)的列(xx)其中数据类型为varchar并且其中包含值,例如'cons','alts'以将其转换以int ... 如果不可能这样做我们有什么其他选项是否有可能在列中已经有数据时更改列的数据类型
更新后的值。事情是这样的:
update t
set column = NULL
where column not like '%[^0-9]%' or column not like '-[^0-9]%';
alter t alter column int;
一旦值是有效的,你应该能够修改类型,在列假设一堆其他条件(涉及限制使用等)。
编辑:
如果你想创建一个参考表,通常你会添加新列。这是讨厌的,因为列被重新排序。所以,你可以使用这个技巧:
create table columnRef (
columnRefId int not null identity primarykey,
value varchar(255) unique
);
insert into columnRef(value)
select distinct column from t;
alter table t add newvalue varchar(255);
update t newvalue = column;
update t
column = cast(columnRefId as varchar(255))
from t join
columnRef cr
on t.newvalue = cr.value;
alter t modify column int;
alter t drop newvalue;
写在这之后,我意识到,你不需要newvalue
。但是,不知何故,我不适合修改用于join
的列中的数据,并且之后无法检查准确性。
不能'cons,alts'不能转换为'INT'。可能只有当**现有数据隐式转换为INT时 –
如果你有一个预定义的对应关系,即cons = 1 alts = 2等,如果你没有那么多的值可能是可能的 – Mihai
如果你不能进行隐式转换,你总是可以使用update语句将数据修改为允许它的值 - 比如cons = 1,ALTs = 2等 – Takarii