How do you change the datatype of a column in SQL Server?

后端 未结 8 746
日久生厌
日久生厌 2020-11-29 16:19

I am trying to change a column from a varchar(50) to a nvarchar(200). What is the SQL command to alter this table?

相关标签:
8条回答
  • 2020-11-29 16:52
    ALTER TABLE TableName 
    ALTER COLUMN ColumnName NVARCHAR(200) [NULL | NOT NULL]
    

    EDIT As noted NULL/NOT NULL should have been specified, see Rob's answer as well.

    0 讨论(0)
  • 2020-11-29 16:59

    Don't forget nullability.

    ALTER TABLE <schemaName>.<tableName>
    ALTER COLUMN <columnName> nvarchar(200) [NULL|NOT NULL]
    
    0 讨论(0)
  • 2020-11-29 17:09

    As long as you're increasing the size of your varchar you're OK. As per the Alter Table reference:

    Reducing the precision or scale of a column may cause data truncation.

    0 讨论(0)
  • 2020-11-29 17:11

    Try this:

    ALTER TABLE "table_name"
    MODIFY "column_name" "New Data Type";
    
    0 讨论(0)
  • 2020-11-29 17:12

    Use the Alter table statement.

    Alter table TableName Alter Column ColumnName nvarchar(100)
    
    0 讨论(0)
  • 2020-11-29 17:14
    ALTER TABLE [dbo].[TableName]
    ALTER COLUMN ColumnName VARCHAR(Max) NULL
    
    0 讨论(0)
提交回复
热议问题