views:

268

answers:

5

In SQL Server 2005 (not 7.0), is there any reason to use NVARCHAR(255) instead of 256 or some other number?
Is there any optimal size, and is there any reason to use powers of two?

(I will be storing Unicode data, so I have to use NVARCHAR)

+1  A: 

The optimal size is the greatest length you will need to store your data.

Andrew Hare
Yes, but I don't know exactly how long it will be, and I'm wondering what to round to. Two or three hundred characters sounds about right.
SLaks
A: 

In SQL 2005+ there is a NVarChar(max) that uses that maximum size available to you. You can store up to 2GB in that. Its the same as NText

It doesnt really matter what you store as they are all pretty much the same

AutomatedTester
+2  A: 

In SQL Server 2005, no. The only reason to use 255 is if you need to consider portability with other platforms. Some other database flavors change the name of the type completely after 255 characters.

Joel Coehoorn
+1  A: 

No, there is no advantage of choosing maximum field lengths that are powers of two.

It's a holdover from the old and crusty days of software development where strings less than 256 characters long were stored one way, and strings over 255 characters another way.

richardtallent
+2  A: 

In SQL Server (2000 and earlier IIRC), the optimal (possibly even maximum) size of columns is dependent on the size of all columns together, excluding (n)text and image contents. I believe 8K is the 'natural' size of records in a table, so your column size added together should be something less than 8000 bytes.

From SQL Server 2005 an higher this isn't much of an issue, I believe.

Ruben
The maximum size of a row in SQL Server is 8060 bytes, according to the maximum capacity specs in BOL.
Chris J