Changing the maximum length of a varchar column?

SqlSql ServerSql Server-2008TsqlVarchar

Sql Problem Overview


I'm trying to update the length of a varchar column from 255 characters to 500 without losing the contents. I've dropped and re-created tables before but I've never been exposed to the alter statement which is what I believe I need to use to do this. I found the documentation here: ALTER TABLE (Transfact-SQL) however I can't make heads or tails of it.

I have the following so far (essentially nothing unfortunately):

alter table [progennet_dev].PROGEN.LE
alter column UR_VALUE_3

How do I approach this? Is there better documentation for this statement out there (I did some searches for an example statement but came up empty)?

Sql Solutions


Solution 1 - Sql

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.

Solution 2 - Sql

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

Solution 3 - Sql

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");

Solution 4 - Sql

ALTER TABLE TABLE_NAME MODIFY COLUMN_NAME VARCHAR(40);

I am using Oracle SQL Developer and @anonymous' answer was the closest, but kept receiving syntax errors until I edited the query to this. I changed alter to modify and there's no need to define column_name as column.

Solution 5 - Sql

Using Maria-DB and DB-Navigator tool inside IntelliJ, MODIFY Column worked for me instead of Alter Column

Solution 6 - Sql

In Oracle SQL Developer

ALTER TABLE car_details MODIFY torque VARCHAR(100);

Solution 7 - Sql

I was also having above doubt, what worked for me is

ALTER TABLE `your_table` CHANGE `property` `property` 
VARCHAR(whatever_you_want) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL;  

Solution 8 - Sql

This worked for me in db2:

alter table "JOBS"  alter column "JOB_TITLE" set  data type varchar(30);

Solution 9 - Sql

As an alternative, you can save old data and create a new table with new parameters.

see image

In SQL Server Management Studio: "your database" => task => generatescripts => select specific database object => "your table" => advanced => types of data to script - schema and data => generate

Personally, I did so.

Solution 10 - Sql

For MariaDB, use modify column:

ALTER TABLE table_name MODIFY COLUMN column_name VARCHAR (500);

It will work.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichael AView Question on Stackoverflow
Solution 1 - SqlMartin SmithView Answer on Stackoverflow
Solution 2 - SqlMitch WheatView Answer on Stackoverflow
Solution 3 - SqlanonymousView Answer on Stackoverflow
Solution 4 - SqlJettView Answer on Stackoverflow
Solution 5 - SqlRahul SharmaView Answer on Stackoverflow
Solution 6 - SqlBiman PalView Answer on Stackoverflow
Solution 7 - SqlR. GurungView Answer on Stackoverflow
Solution 8 - SqlMukeshView Answer on Stackoverflow
Solution 9 - SqlvladView Answer on Stackoverflow
Solution 10 - SqlhammadshahirView Answer on Stackoverflow