soso
soso

Reputation:

sql server data fields nvchar(x) or nvarchar(max)

Is it good practice to set all text fields to nvarchar(MAX)? if im not sure what the size of the field will be

will it take up extra space

Upvotes: 1

Views: 431

Answers (3)

Andomar
Andomar

Reputation: 238116

varchar(max) won't take extra space.

One dangerous thing about varchar(max) is that someone can potentially but a huge amount of data in it. This can hurt if a malicious user. or faulty program, enters a 1MB address line. No client or web application will expect that, and it might cause them to crash.

So, as a best practice, create varchar fields with a specified maximum length.

Upvotes: 1

user108736
user108736

Reputation: 11

I'm not sure what you're storing in the columns but you may want to consider varchar instead of nvarchar. nvarchar stores unicode data which is used for multilingual data and takes up more space than varchar. If the field will only store English (for example) then varchar might be a better choice.

Upvotes: 1

devstuff
devstuff

Reputation: 8387

If you're not sure, and its for something like a comments field that may be quite large, a NVARCHAR(MAX) type is the way to go. If it is for something with a reasonable maximum, like a postal address, then something like NVARCHAR(100) might be better.

The space used is up to the maximum length of the value that is put into it.

What I mean by that is if you insert an 80 character string, then update it with a 40 character replacement, the allocated space may remain as 80 characters until the database is compacted, table is rebuilt, etc.

Upvotes: 1

Related Questions