Raymond Lewallen

Sponsors

The Lounge

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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.


Posted 12-30-2005 9:04 AM by Raymond Lewallen

[Advertisement]

Comments

Raymond Lewallen wrote Beating the 8K row size limitation in Sql Server 2005
on 12-30-2005 10:09 AM

Here I talked about row size limitations and using varchar versus nvarchar data in Sql Server. ...
Jason Haley wrote Interesting Finds
on 12-31-2005 8:35 AM
Josh wrote re: Database Basics Quick Note - The difference in Varchar and Nvarchar data types
on 01-03-2006 9:06 AM
You might want to qualify your definition of "normal use". Any application that wants to support global usage should probably use nvarchar.
Raymond Lewallen wrote re: Database Basics Quick Note - The difference in Varchar and Nvarchar data types
on 01-03-2006 9:13 AM
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.
Haacked wrote re: Database Basics Quick Note - The difference in Varchar and Nvarchar data types
on 01-06-2006 9:14 PM
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.
asr wrote re: Database Basics Quick Note - The difference in Varchar and Nvarchar data types
on 04-04-2007 2:12 PM
Sayed Moawad wrote re: Database Basics Quick Note - The difference in Varchar and Nvarchar data types
on 07-31-2008 8:31 AM

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 .

Syed Akhtar wrote re: Database Basics Quick Note - The difference in Varchar and Nvarchar data types
on 08-05-2008 11:30 PM

Its been  a nice way to deliver the difference. Thnx

Bhoopendra wrote re: Database Basics Quick Note - The difference in Varchar and Nvarchar data types
on 10-12-2008 3:29 PM

Thanks

Sami wrote re: Database Basics Quick Note - The difference in Varchar and Nvarchar data types
on 01-21-2009 2:50 PM

Thanks, still useful the info.

Especially when I look at my  leg and understood why.

KG wrote re: Database Basics Quick Note - The difference in Varchar and Nvarchar data types
on 03-16-2009 5:28 PM

Good stuff,

Thanks for the info.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?