Click here to monitor SSC

John Magnabosco

SQL Server Development and Data Security

Varchar and NVarchar

Published Friday, August 21, 2009 5:31 AM

Another trip with Mr. Peabody's WABAC Machine unearthed a valuable blog entry that I posted before my Simple-Talk blogging days. This one is somewhat of a "Back to the basics" style blog entry that I thought would be worth sharing once again. Enjoy!

If you peruse quotes and philosophies of very successful people it will not take long to see that revisiting the basics of a discipline is something that is critical to excelling; thus, I thought that it would be a good idea to comment on the varchar and nvarchar data types.

According to SQL Server 2005 Books Online the varchar(n) data type is described as:
"Variable-length, non-Unicode character data. n can be a value from 1 through 8,000."

According to SQL Server 2005 Books Online the nvarchar(n) data type is described as:
"Variable-length Unicode character data. n can be a value from 1 through 4,000."

The "n" referred to above is the defined maximum size of the data type. For example declaring the column in your table as varchar(50) will mean that the column will store up to 50 characters. For the untrained eye the immediate impression may be that varchar can store twice the data of nvarchar. In actuality both data types store the same number of bytes - it is the number of characters that differ. Within the BOL definitions above the key word to pay attention to is "Unicode".

Non-Unicode characters are stored in a single byte. For example: "A" would be encoded as an ASCII value of "65". These single byte values range from 0 - 255 which are represented in an ASCII table. The following link is a good resource for these values: http://www.asciitable.com/

In reviewing the ASCII table you will find that if you are using languages that utilize characters that are not included in the ASCII table (such as Japanese, Chinese, Korean, etc.) it can be rather limiting.

Unicode characters are encoded in two bytes (double-byte). For example: "A" would be stored as a value of "A". These double-byte characters have 65,536 combinations which accommodates majority, if not all, languages in the world. A good tool to convert ASCII text to Unicode text can be found at this link: http://www.industrialtrainer.com/Unicode.shtm

If the database that is being designed does not need to consider character sets beyond the ASCII set the use of varchar would be the more efficient storage option; but in this world of ever growing globalization and need to accommodate various character sets it may be wise to consider the strategic utilization of nvarchar for select columns.

by Johnm

Comments

 

Arjan’s World » LINKBLOG for August 21, 2009 said:

August 21, 2009 9:50 AM
 

Twitter Trackbacks for John Magnabosco : Varchar and NVarchar [simple-talk.com] on Topsy.com said:

August 21, 2009 2:28 PM
 

Charles Kincaid said:

Except for one fact.  In the SQL Server Compact Edition as used on Mobile computers VARCHAR and CHAR doe not exist.  One is then stuck with NVARCHAR or NCHAR.  If you use replication or Sync Services to push the data to the Mobile then you are confronted with making you datatypes uniform across platforms or writing some serious translation code.
August 24, 2009 12:50 PM
 

Topaz Discount Dejpeg Image Editor, Topaz Fog Light Rear Honda said:

May 20, 2010 8:14 PM
 

500sec Radiator Discount Catalog, 500sec New Automotive said:

May 20, 2010 11:01 PM
 

2002 Volvo V40 Reliability, Sunfire Free Shipping Sun Fire V40z said:

May 21, 2010 1:00 AM
 

Sequoia Lodgepole Campground, Sequoia Performance Parts Brake Pads said:

May 21, 2010 4:24 AM
 

2004 Vw Jetta Headlight Bulb Type, Jetta Fan Leak said:

May 21, 2010 3:54 PM
 

400se Sale Mercedes Benz 240d Slk32 Amg, 1983 Mercedes 240d Parts Brake Rotors Power Steering Pump said:

May 21, 2010 7:16 PM
 

Falcon Investment, 1964 Ford Falcon Parts Convertible said:

May 21, 2010 11:07 PM
 

Buy 350sdl Cylinder Head 1990 Mercedes Benz, Sale Cars Mercedes Benz 350sdl said:

May 22, 2010 5:43 AM
 

E 150 Econoline Discount Explorer Sport Trac Ford Freestar, Ford Freestar Pontiac Trans Sport said:

May 22, 2010 2:14 PM
 

2006 Chevrolet Silverado 1500 Ls2, 1500 Gmc Sierra Hd Car Parts Salvage Yards said:

May 22, 2010 5:17 PM
 

National Express Phone, 2005 Dodge Durango Express For Sale said:

May 22, 2010 8:28 PM
 

P20 Molding Chrome Moly, P20 Online Parts - 480.rkwrh.com said:

May 23, 2010 5:52 AM
 

Ip1500 Part S15 Jimmy Chevrolet, P15 P1500 Car Part 1967 Gnc - 428.defutbolazo.com said:

May 24, 2010 6:52 AM
 

Sc430 Upgrade Celeron D, Repair C43 - 46.binggreen.com said:

May 24, 2010 12:32 PM
 

New Vw Beetle Sale Uk, Beetle Headlight Electrical - 458.jeepsunlimted.com said:

May 25, 2010 7:41 PM
You need to sign in to comment on this blog

About Johnm

John Magnabosco manages the Data Services Group at one of the fastest growing companies in the United States. He is also a Co-Founder of the Indianapolis Professional Association for SQL Server (IndyPASS), Co-Founder of IndyTechFest, the author of the book titled "Protecting SQL Server Data" and contributing author of "SQL Server MVP Deep Dives Volume 2".
<August 2009>
SuMoTuWeThFrSa
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345
How to Kill a Company in One Step or Save it in Three
 The majority of companies that suffer a major data loss subsequently go out of business. Wesley David... Read more...

Migrating from OCS 2007 R2 to Lync: Part 4
 Having migrated the rest of our users and legacy resources across and started getting ready to... Read more...

Automated Script-generation with Powershell and SMO
 In the first of a series of articles on automating the process of building, modifying and copying SQL... Read more...

Seth Godin: Big in the IT Business
 Seth Godin has transformed our understanding of marketing in IT. He invented the concept of 'permission... Read more...

Using SQL Test Database Unit Testing with TeamCity Continuous Integration
 With database applications, the process of test and integration can be frustratingly slow because so... Read more...