Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting nvarchar(4000) to nvarchar(max)

We have a few tables in our SQL Server (2005 & 2008) database with columns defined as nvarchar(4000). We sometimes need to be able to store more data than that and consider to use nvarchar(max) instead. Now to the questions.

  1. Are there any performance implications we should know of?
  2. Is it safe to use "alter table" to do the actual migration or is there another way?
  3. Anything else we should know before we convert?

Regards Johan

like image 264
Johan Hammar Avatar asked Jun 02 '10 13:06

Johan Hammar


People also ask

Is nvarchar 4000 the same as nvarchar Max?

The answers is: there is no different between nvarchar(7) and nvarchar(4000) in term of performance & storage size. There is an interesting thing is that: if you change nvarchar(7) or nvarchar(4000) to nvarchar(max). There is a difference in term of performance & storage size. Wow, Why is this happen?

Is varchar Max same as varchar 8000?

CHAR, VARCHAR, and VARCHAR(MAX) CHAR columns should be used for columns that vary little in length. String values that vary significantly in length and are no longer than 8,000 bytes should be stored in a VARCHAR column. If you have huge strings (over 8,000 bytes), then VARCHAR(MAX) should be used.

How do I store more than 4000 characters in SQL?

The storage size, in bytes, is two times the actual length of data entered + 2 bytes. The ISO synonyms for nvarchar are national char varying and national character varying. So if you specify nvarchar(max) you can store up to 1 billion 2-byte Unicode characters.

How do I convert nvarchar?

Syntax: SELECT CONVERT(<DATA_TYPE>, <VALUE>); --DATA_TYPE is the type we want to convert to. --VALUE is the value we want to convert into DATA_TYPE. Example: SELECT 'Weight of Yogesh Vaishnav is ' + CONVERT(NVARCHAR(20), weight) AS person_weight FROM person WHERE name = 'Yogesh Vaishnav';


1 Answers

We've had to do this as well in a few spots

  1. We didn't see any performance hits, its been 5 weeks since the change
  2. 'Alter table' is fine as long as the table is not getting written to during the conversion
  3. Be sure to have a backup just in case and depending on the amount data in the table it could take a while
like image 60
Jason Avatar answered Sep 21 '22 05:09

Jason