Convert column nText to nvarchar(1026)

时间:2015-11-12 11:21:12

标签: sql-server

I need to change my column datatype from nText null to nvarchar(1026) null. I have used the below query(Sql server).

ALTER TABLE [Tablename] ALTER COLUMN [Columnname] nvarchar(1026) null

while executing the query, I am getting the following issue:

Cannot alter column of type NTEXT or IMAGE [ Column Name = Columnname]

Any help would be appreciated ! Thanks in advance :)

1 个答案:

答案 0 :(得分:3)

Your SQL statement is working fine in Sql Server 2014.

I believe you have to perform several steps.

  1. Create a new column in your table

    ALTER TABLE [Tablename] ADD [NewCol] nvarchar(1026) null
    
  2. Copy original column content to the new column using UPDATE and CONVERT

    UPDATE [Tablename] SET NewCol=CONVERT(nvarchar(1026), Columnname)
    
  3. Drop original column

    ALTER TABLE [Tablename] drop column [Columnname]
    
  4. Rename new column back to the original name

    Exec sp_rename 'Tablename.NewCol', 'Columnname', 'Column'