Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 937
Ascalonian Avatar asked Mar 09 '09 16:03

Ascalonian


People also ask

Can we change datatype of column?

We can use ALTER TABLE ALTER COLUMN statement to change the datatype of the column. The syntax to change the datatype of the column is the following. In the syntax, Tbl_name: Specify the table name that contains the column that you want to change.

How do you change the datatype of a column in SQL without losing data?

So to do that go to SQL Server and within Tools select Options. Now in the option window expand Designers and under that "Table and Database Designers" and uncheck the check box "Prevent saving changes that require table re-creation" then click OK.

Which command is used to change the data type of a column in the SQL table?

The ALTER COLUMN command is used to change the data type of a column in a table.


2 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.

like image 54
cmsjr Avatar answered Sep 24 '22 14:09

cmsjr


Don't forget nullability.

ALTER TABLE <schemaName>.<tableName> ALTER COLUMN <columnName> nvarchar(200) [NULL|NOT NULL] 
like image 39
Rob Garrison Avatar answered Sep 23 '22 14:09

Rob Garrison