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

Viewed 786334

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

11 Answers
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.

Don't forget nullability.

ALTER TABLE <schemaName>.<tableName>
ALTER COLUMN <columnName> nvarchar(200) [NULL|NOT NULL]

Use the Alter table statement.

Alter table TableName Alter Column ColumnName nvarchar(100)

For changing data type

alter table table_name 
alter column column_name datatype [NULL|NOT NULL]

For changing Primary key

ALTER TABLE table_name  
ADD CONSTRAINT PK_MyTable PRIMARY KEY (column_name)
ALTER TABLE [dbo].[TableName]
ALTER COLUMN ColumnName VARCHAR(Max) NULL

in 11g:

ALTER TABLE TableName
Modify ColumnName DataType;

EG: ALTER TABLE employees Modify BIRTH_DATE VARCHAR(30);

ALTER TABLE [Performance].[dbo].[CRA_283_Violation]
ALTER COLUMN [Throughput_HS_DC_NodeB_3G_Alarm] bit

The original question is for 'SQL Server' However, in the case you read this post when you are with a MySql Server, the 'ALTER COLUMN' cannot be used to change the column type.

To change the column type on a MYSQL server, you can use:

ALTER TABLE `USER`
    MODIFY SESSION_ID VARCHAR(100) NULL;

I used the line above to extend from varchar(20) to varchar(100) the column with a MySql Server.

Related