405

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

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Ascalonian
  • 14,409
  • 18
  • 71
  • 103

12 Answers12

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

siddhartha jain
  • 1,006
  • 10
  • 16
cmsjr
  • 56,771
  • 11
  • 70
  • 62
  • 2
    Why should it be specified? What is the benefit? Everything I want to leave as it is I don't have to mention, I think. – The incredible Jan Mar 10 '17 at 07:48
  • 8
    @TheincredibleJan That would be cool, but unfortunately it doesn't work that way. If you issue an `ALTER TABLE TableName ALTER COLUMN ColumnName` command to change an existing column that is `[NOT NULL]`, and you don't explicitly specify it, it will be `[NULL]` afterwards, as that's the default. – takrl Apr 19 '17 at 06:10
  • 4
    This only works if you don't have constraints and indexes on that column, otherwise, you need to drop everything and recreate it, which is tedious and grunt work, especially if you have many foreign references on that column. e.g. when changing from tinyint to int. – Hrvoje Batrnek Jun 09 '18 at 14:18
  • Careful if you have an IDE open (like SSMS). Even with my Designer Tab closed on the Table I was Altering (after running the script and right-clicking on the Table to select "Design") it still showed the old Data Types! It was only after closing ALL of my Tabs in Management Studio and opening the Design View again that it finally showed the updated DataType. Very scary, so be careful (it may be a cache-bug that is fixed by now or one MS never bothers fixing). For those wondering why I ran T-SQL when I use Design-View, I wanted to alter my DataTypes to `SysName` (which SSMS does not allow). – MikeTeeVee Apr 10 '19 at 02:37
  • I had no luck with these commands in XAMPP.`ALTER TABLE table MODIFY COLUMN column datatype` worked for me. – Pavindu May 06 '19 at 14:28
  • Pavindu, that is because MODIFY / XAMPP is not T-SQL (SQLServer). That is MySQL, which is different. – Eddy Howard Dec 11 '19 at 18:58
193

Don't forget nullability.

ALTER TABLE <schemaName>.<tableName>
ALTER COLUMN <columnName> nvarchar(200) [NULL|NOT NULL]
Rob Garrison
  • 6,984
  • 4
  • 25
  • 23
26

Use the Alter table statement.

Alter table TableName Alter Column ColumnName nvarchar(100)
John Sansom
  • 41,005
  • 9
  • 72
  • 84
13

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

pbaris
  • 4,525
  • 5
  • 37
  • 61
Yogesh Bende
  • 139
  • 1
  • 2
7
ALTER TABLE [dbo].[TableName]
ALTER COLUMN ColumnName VARCHAR(Max) NULL
Umar Asif
  • 151
  • 2
  • 10
  • 3
    This doesn't seem to add anything over the existing accepted answer from years ago, and also the changes you've posted to the table structure don't actually match the question. – PeterJ Mar 05 '20 at 11:05
5

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)
Alexander Zaldostanov
  • 2,907
  • 4
  • 30
  • 39
4

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.

jocassid
  • 4,429
  • 1
  • 13
  • 6
1

in 11g:

ALTER TABLE TableName
Modify ColumnName DataType;

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

Biman Pal
  • 391
  • 4
  • 9
-1
ALTER TABLE [Performance].[dbo].[CRA_283_Violation]
ALTER COLUMN [Throughput_HS_DC_NodeB_3G_Alarm] bit
mxmissile
  • 11,464
  • 3
  • 53
  • 79
Babimetro
  • 19
  • 2
-1

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.

Thierry Brémard
  • 625
  • 1
  • 5
  • 14
-1

For postgres sql it will be:

ALTER TABLE "TableName" ALTER "ColumnName" TYPE DataType;
Bartosz546
  • 95
  • 5
-13

Try this:

ALTER TABLE "table_name"
MODIFY "column_name" "New Data Type";
Marius
  • 15,148
  • 9
  • 56
  • 76
Kai Tzer
  • 53
  • 5