Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alter Field Type from nvarchar(255) to nvarchar(Max) in SQL Server 2005

I would like to alter column type in table stk020. So, I used this query..

ALTER TABLE [stk020] 
ALTER COLUMN [t16] nvarchar(Max) not null 

The original column type of [t16] is nvarchar(255). I get this error

Msg 5074, Level 16, State 1, Line 1
The object 'DF_STK020_t16__725CC34D' is dependent on column 't16'.
Msg 4922, Level 16, State 9, Line 1
ALTER TABLE ALTER COLUMN t16 failed because one or more objects access this column.

Any solution?

like image 606
suhtite Avatar asked Jun 04 '13 04:06

suhtite


People also ask

How many characters is nvarchar 255?

nvarchar(255) (in SQL Server) stores 255 Unicode characters (in 510 bytes plus overhead).

How do I change nvarchar length in SQL?

You can use the ALTER table command to change the length of a varchar column. You can increase the length of a varchar column to a maximum size of 64,000.

How big is nvarchar 255?

The minimum size of the NVARCHAR value is 1 byte. The total length of an NVARCHAR column cannot exceed 255 bytes. You can store character strings that are shorter - but not longer - than the specified value. You must always specify the max parameter.

What is the difference between nvarchar 50 and nvarchar Max?

nvarchar max is for columns up to 2GB. So essentially it takes up more resources. You are better off using the nvarchar(50) if you know you aren't going to need that much space. each character is about 2 bytes so with 2 GB thats 1 billion characters...


1 Answers

You must first drop what we presume is the Default constraint on the column before you alter its data type:

Alter Table stk020 Drop Constraint DF_STK020_t16__725CC34D
GO
Alter Table stk020 Alter Column t16 nvarchar(max) not null
GO
Alter Table stk020 Add Constraint DF_STK020_t16__725CC34D
    Default ... For t16
like image 79
Thomas Avatar answered Sep 27 '22 16:09

Thomas