Changing the maximum length of a varchar column? Changing the maximum length of a varchar column? sql-server sql-server

Changing the maximum length of a varchar column?


You need

ALTER TABLE YourTable ALTER COLUMN YourColumn <<new_datatype>> [NULL | NOT NULL]

But remember to specify NOT NULL explicitly if desired.

ALTER TABLE YourTable ALTER COLUMN YourColumn VARCHAR (500) NOT NULL;

If you leave it unspecified as below...

ALTER TABLE YourTable ALTER COLUMN YourColumn VARCHAR (500);

Then the column will default to allowing nulls even if it was originally defined as NOT NULL. i.e. omitting the specification in an ALTER TABLE ... ALTER COLUMN is always treated as.

ALTER TABLE YourTable ALTER COLUMN YourColumn VARCHAR (500) NULL;

This behaviour is different from that used for new columns created with ALTER TABLE (or at CREATE TABLE time). There the default nullability depends on the ANSI_NULL_DFLT settings.


Increasing column size with ALTER will not lose any data:

alter table [progennet_dev].PROGEN.LE     alter column UR_VALUE_3 varchar(500) 

As @Martin points out, remember to explicitly specify NULL | NOT NULL


For MySQL or DBMSes other than MSSQL, you may need to use modify instead of alter for the column value:

ALTER TABLE `table name` modify COLUMN `column name` varchar("length");