How do you change the datatype of a column in SQL Server?
Sql ServerTsqlType ConversionAlter TableSql Server Problem Overview
I am trying to change a column from a varchar(50)
to a nvarchar(200)
. What is the SQL command to alter this table?
Sql Server Solutions
Solution 1 - Sql Server
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.
Solution 2 - Sql Server
Don't forget nullability.
ALTER TABLE <schemaName>.<tableName>
ALTER COLUMN <columnName> nvarchar(200) [NULL|NOT NULL]
Solution 3 - Sql Server
Use the Alter table statement.
Alter table TableName Alter Column ColumnName nvarchar(100)
Solution 4 - Sql Server
The syntax to modify a column in an existing table in SQL Server (Transact-SQL) is:
ALTER TABLE table_name
ALTER COLUMN column_name column_type;
For example:
ALTER TABLE employees
ALTER COLUMN last_name VARCHAR(75) NOT NULL;
This SQL Server ALTER TABLE
example will modify the column called last_name
to be a data type of VARCHAR(75)
and force the column to not allow null values.
see here
Solution 5 - Sql Server
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)
Solution 6 - Sql Server
As long as you're increasing the size of your varchar you're OK. As per the [Alter Table][1] reference:
> Reducing the precision or scale of a column may cause data truncation.
[1]: http://msdn.microsoft.com/en-us/library/ms190273.aspx "Alter Table"
Solution 7 - Sql Server
ALTER TABLE [dbo].[TableName]
ALTER COLUMN ColumnName VARCHAR(Max) NULL
Solution 8 - Sql Server
in 11g:
ALTER TABLE TableName
Modify ColumnName DataType;
EG: ALTER TABLE employees Modify BIRTH_DATE VARCHAR(30);
Solution 9 - Sql Server
ALTER TABLE [Performance].[dbo].[CRA_283_Violation]
ALTER COLUMN [Throughput_HS_DC_NodeB_3G_Alarm] bit
Solution 10 - Sql Server
Try this:
ALTER TABLE "table_name"
MODIFY "column_name" "New Data Type";