Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Varchar(255) to Varchar(MAX)

Tags:

types

sql

varchar

Is it possible to change a column type in a SQL Server 2008 database from varchar(255) to varchar(MAX) without having to drop the table and recreate?

SQL Server Management Studio throws me an error every time I try to do it using that - but to save myself a headache would be nice to know if I can change the type without having to DROP and CREATE.

Thanks

like image 737
Darbio Avatar asked Apr 23 '10 03:04

Darbio


People also ask

Is varchar 255 the max?

Storage Information : The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used. Make sure you are aware of the effects of a multi-byte character set. VARCHAR(255) stores 255 characters, which may be more than 255 bytes.

What size is varchar Max?

The VARCHAR(MAX) data type is similar to the VARCHAR data type in that it supports variable-length character data. VARCHAR(MAX) is different from VARCHAR because it supports character strings up to 2 GB (2,147,483,647 bytes) in length.

Why do we write varchar 255 in SQL?

1 byte for the length, and the actual storage for the rest. 255 is the max limit, not an allocation. 255 is not the max limit. There is a variable MAX you can use which Varchar(max) stores a maximum of 2 147 483 647 characters.

How many char is varchar Max?

Microsoft SQL Server 2008 (and above) can store up to 8000 characters as the maximum length of the string using varchar data type. SQL varchar usually holds 1 byte per character and 2 more bytes for the length information.


2 Answers

You should be able to do it using TSQL.

Something like

ALTER TABLE [table] ALTER COLUMN [column] VARCHAR(MAX) 
like image 58
Adriaan Stander Avatar answered Oct 09 '22 13:10

Adriaan Stander


'Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created. You have either made changes to a table that can't be re-created or enabled the option Prevent saving changes that require table to be re-created.' Option 'Prevent saving changes' is not enabled..

That's a new "feature" in SQL Server Management Studio 2008 which by default is turned on. Whenever you make a larger change, SSMS can only recreate the table by creating a new one and then moving over the data from the old one - all in the background (those changes include re-ordering of your columns amongst other things).

This option is turned off by default, since if your table has FK constraints and stuff, this way of re-doing the table might fail. But you can definitely turn that feature on!

alt text

It's under Tools > Options and once you uncheck that option you can do these kind of changes to table structure in the table designer again.

like image 32
marc_s Avatar answered Oct 09 '22 11:10

marc_s