简体   繁体   English

SQL将列数据类型从nvarchar更改为int

[英]SQL alter column datatype from nvarchar to int

Can the datatype of a field be changed to int from nvarchar?? 可以将字段的数据类型从nvarchar更改为int吗?

alter table employee alter column designation int

is this valid?? 这有效吗? If not can it be done in some other way?? 如果不能,可以通过其他方式完成吗?

PS: I am using MS SQL Server PS:我正在使用MS SQL Server

You can try doing an alter table. 您可以尝试做一个变更表。 If it fails do this: 如果失败,请执行以下操作:

  1. Create a new column that's an integer: 创建一个新的整数列:

ALTER TABLE tableName ADD newCol int;

  1. Select the data from the old column into the new one: 从旧列中选择数据到新列中:

UPDATE tableName SET newCol = CAST(oldCol AS int) ; UPDATE tableName SET newCol = CAST(oldCol AS int) ;

  1. Drop the old column 删除旧列

It is possible only when you column has no value or blank. 仅当您的列没有值或空白时才有可能。 If your column has some value which have nvarchar value and you should try to convert it into int, it will give error. 如果您的列具有一些具有nvarchar值的值,并且您应尝试将其转换为int,它将给出错误信息。

ALTER TABLE [table_name] ALTER COLUMN [column_name] [data_type]
  1. Add new numeric column. 添加新的数字列。
  2. Copy from old char column to new column with trim and conversion. 通过修剪和转换从旧的char列复制到新列。
  3. Drop old char column. 删除旧字符列。
  4. Rename numeric column to old column name. 将数字列重命名为旧列名。

This worked for me (with decimals but I suppose it will work with ints): 这对我有用(带小数点,但我想它将与整数一起工作):

alter table MyTable add MyColNum decimal(15,2) null
go
update MyTable set MyColNum=CONVERT(decimal(15,2), REPLACE(LTRIM(RTRIM(MyOldCol)), ',', '.')) where ISNUMERIC(MyOldCol)=1
go
alter table MyTable drop column MyOldCol
go
EXEC sp_rename 'MyTable.MyColNum', 'MyOldCol', 'COLUMN'
go

Can be done even simpler in just 2 steps 只需2个步骤即可完成

  1. Update the column and set all non numberic values to null so alter won't fail. 更新该列并将所有非数字值设置为null,这样alter不会失败。

  2. Alter the table and set the type to int. 更改表并将类型设置为int。

UPDATE employee
SET designation = (CASE WHEN ISNUMERIC(designation)=1 THEN CAST(CAST(designation AS FLOAT) AS INT)END )

ALTER TABLE employee
ALTER COLUMN designation INT

This takes the assumption that that the columns allow nulls. 假设列允许空值。 If not then that needs to be handled as well. 如果没有,那也需要处理。 For example: By altering the column to allow null, then after it has been converted to int then set all null values to 0 and alter the table to not allow null 例如:通过将列更改为允许null,然后将其转换为int之后,将所有null值设置为0,并将表更改为不允许null

  1. Create a temp column 创建一个临时列

    ALTER TABLE MYTABLE ADD MYNEWCOLUMN NUMBER(20,0) NULL;

  2. Copy and casts the data from the old column to the new one 将数据从旧列复制并转换为新列

    UPDATE MYTABLE SET MYNEWCOLUMN=CAST(MYOLDCOLUMN AS NUMBER(20,0));

  3. Delete the old column 删除旧列

    ALTER TABLE MYTABLE DROP COLUMN MYOLDCOLUMN;

  4. Rename the new one to match the same name as the old one. 重命名新名称,使其与旧名称相同。

    ALTER TABLE MYTABLE RENAME COLUMN MYNEWCOLUMN TO MYOLDCOLUMN;

Can you try this ? 你可以试试这个吗?

alter table MyTable add MyColNum Varchar(500) null;
alter table MyTable add MyColNum int null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM