CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Raymond Lewallen

Professional Learner

Database Basics Quick Note - The difference in Varchar and Nvarchar data types

I saw the following question posted on a forum and left the answer for the poster.  I thought I would share it with you all as I am in the process of the Database Basics posts, and this ties in.

“What is the difference between nvarchar and varchar data types?”

The difference in varchar and nvarchar datatypes is simple.  Nvarchar stores UNICODE data.  If you have requirements to store UNICODE or multilingual data, nvarchar is your choice.  Varchar stores ASCII data and should be your data type of choice for normal use.

UNICODE requires 2 bytes for each character you store.  ASCII only requires 1 byte for each character.  This is important because of the row size limitations of Sql Server is the same as the page size limit, which is 8060 bytes.  This means a single row of a single varchar column can be varchar(8000), but a single row of a single nvarchar column can only be nvarchar (4000).

Now, you can actually go and create a table with 5 columns of varchar(5000), and Sql Server will let you do that.  But once you go to put data into it, its going to puke down your leg.  This goes back to the size of the data page, which is 8K.  Rows cannot span pages (this does not apply to Sql Server 2005), so until the page size is increased, you have to adhere to that limitation.

Also, you might bring up the argument of Text and Image fields.  This data is stored seperately and a 24 byte pointer is stored in the original data page.



Comments

Raymond Lewallen said:


Here I talked about row size limitations and using varchar versus nvarchar data in Sql Server. ...
# December 30, 2005 10:09 AM

Jason Haley said:

# December 31, 2005 8:35 AM

Josh said:

You might want to qualify your definition of "normal use". Any application that wants to support global usage should probably use nvarchar.
# January 3, 2006 9:06 AM

Raymond Lewallen said:

Josh, I attempted to qualify the statement with "multilingual data" mentioned in the second paragraph. Your comment will probably make it more clear, thank you.
# January 3, 2006 9:13 AM

Haacked said:

These days, if you're working on a web app that's not for personal use, you can almost be sure you'll need to support multiple languages. Even intranets often need it as a company opens up offices in other countries.

For any columns that gets displayed to the user, I'd probably recommend going ahead and making it an nvarchar, since changing it later when the table is really big can be a big pain.
# January 6, 2006 9:14 PM

Sayed Moawad said:

VarChar : A variable-length byte array of non-Unicode (256 cod epage) characters ranging between 1 and 2^64 (1-8000) characters.

NVarChar :A variable-length byte array of Unicode characters ranging between 1 and 2^63 (1-4000) characters .

# July 31, 2008 8:31 AM

Syed Akhtar said:

Its been  a nice way to deliver the difference. Thnx

# August 5, 2008 11:30 PM

Bhoopendra said:

Thanks

# October 12, 2008 3:29 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Raymond Lewallen

Working primarily in the public sector during his career, Raymond has designed and built several high profile enterprise level applications for all levels of the government. Raymond now works as a solutions architect for EMC. Raymond is an agile coach, Microsoft MVP C# and also president of the Oklahoma City Developers Group and Oklahoma Agile Developers Group. Raymond spends a lot of his time learning and teaching such things as Test Driven Development, Domain Driven Design, Design Patterns and Extreme Programming practices and principles, to name a few. Raymond is also an advocate of Alt.Net. Raymond is primarily a framework guy, so don't ask him anything about UI :) Check out Devlicio.us!

Our Sponsors