Skip to content

NVARCHAR(n) Vrs NVARCHAR(max)

May 28, 2012

These datatypes particularly NVARCHAR(n), have been with us for some time now but having seen some of the confusion around when to use any of these, I feel oblige to share and infact to also learn more from anyone out there.

 

NVARCHAR(n)

This data is used for storing a variable-length unicode string. The variable, n, – for the mathematically inclined – denotes the length of the string data and this be anything between 1 and 4,000.

 

NVARCHAR(max) (Large value type.)

The “max” in this datatype denotes a maximum storage size of 2 GB. Values stored in a nvarchar(N) are physically stored in the same way. However for the nvarchar(max) datatype , the values are treated as a TEXT value thus some additional processing is required particularly when the size exceeds 8000.

 

So when do we use any of these

The choice of which of these data types to use, firstly lies with what the business requirements are. In my own experience, the nvarchar(n) data type has been the preferred option as per requirements and more importantly, because of performance issues.

Unless there is a compelling reason to use nvarchar(max), I will strongly recommend the use of nvarcha(n) as this will meet most business requirement and also the optimum performance one stands to gain from this. For instance, creating a field just to store any amount of data only for it hold 100 or even 200 characters is not elegant.

Hope you find this article useful, please feel free to add comments and let me know what you think.

cheers

Advertisements

From → SQL

One Comment
  1. Gill Bates permalink

    INDEXES!! You can’t create indexes on varchar fields over a certain length

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: