views:

622

answers:

9

I am storing first name and last name with up to 30 characters each. Which is better varchar or nvarchar.

I have read that nvarchar takes up twice as much space compared to varchar and that nvarchar is used for internationalization.

So what do you suggest should I use: nvarchar or varchar ?

Also please let me know about the performance of both. Is performance for both is same or they differ in performance. Because space is not too big issue. Issue is the performance.

+6  A: 

First of all, to clarify, nvarchar stores unicode data while varchar stores ANSI (8-bit) data. They function identically but nvarchar takes up twice as much space.

Generally, I prefer storing user names using varchar datatypes unless those names have characters which fall out of the boundary of characters which varchar can store.

It also depends on database collation also. For e.g. you'll not be able to store Russian characters in a varchar field, if your database collation is LATIN_CS_AS. But, if you are working on a local application, which will be used only in Russia, you'd set the database collation to Russian. What this will do is that it will allow you to enter Russian characters in a varchar field, saving some space.

But, now-a-days, most of the applications being developed are international, so you'd yourself have to decide which all users will be signing up, and based on that decide the datatype.

Kirtan
+2  A: 

I have red that nvarchar takes twice as varchar.

Yes.

nvarchar is used for internationalization.

Yes.

what u suggest should i use nvarchar or varchar?

It's depends upon the application.

adatapost
Not correct. An nvarchar needs twice as much space as a varchar. You can easily check this using the DATALENGTH function.
Guffa
Thanks Kirtan. I apologize for the inconvenience caused.
adatapost
+11  A: 

Basically, nvarchar means you can handle lots of alphabets, not just regular English. Technically, it means unicode support, not just ANSI. This means double-width characters or approximately twice the space. These days disk space is so cheap you might as well use nvarchar from the beginning rather than go through the pain of having to change during the life of a product.

If you're certain you'll only ever need to support one language you could stick with varchar, otherwise I'd go with nvarchar.

This has been discussed on SO before here.

EDITED: changed ascii to ANSI as noted in comment.

dave
being nitpicky: actually, VARCHAR stores ANSI data - 8-bit, typically based on a code page like Western European or Icelandic or whatever you need :-) It's ANSI - 8-bit - not ASCII (= 7-bit)
marc_s
+1  A: 

By default go with nvarchar. There is very little reason to go with varchar these days, and every reason to go with nvarchar (allows international characters; as discussed).

Noon Silk
+1  A: 

varchar is 1 byte per character, nvarchar is 2 bytes per character.

You will use more space with nvarchar but there are many more allowable characters. The extra space is negligible, but you may miss those extra characters in the future. Even if you don't expect to require internationalization, people will often have non-English characters (e.g. é, ñ or ö) in their names.

I would suggest you use nvarchar.

Kirk Broadhurst
A: 

The nvarchar type is Unicode, so it can handle just about any character that exist in every language on the planet. The characters are stored as UTF-16 or UCS-2 (not sure which, and the differences are subtle), so each character uses two bytes.

The varchar type uses an 8 bit character set, so it's limited to the 255 characters of the character set that you choose for the field. There are different character set that handles different character groups, so it's usually sufficient for text local to a country or a region.

If varchar works for what you want to do, you should use that. It's a bit less data, so it's overall slightly faster. If you need to handle a wide variety of characters, use nvarchar.

Guffa
A: 

I have red that nvarchar takes twice as varchar

Yes. According to Microsoft: "Storage size, in bytes, is two times the number of characters entered + 2 bytes" (http://msdn.microsoft.com/en-us/library/ms186939%28SQL.90%29.aspx).

But storage is cheap; I never worry about a few extra bytes.

Also, save yourself trouble in the future and set the maximum widths to something more generous, like 100 characters. There is absolutely no storage overhead to this when you're using varchar or nvarchar (as opposed to char/nchar). You never know when you're going to encounter a triple-barrelled surname or some long foreign name which exceeds 30 characters.

nvarchar is used for internationalization.

nvarchar can store any unicode character, such as characters from non-Latin scripts (Arabic, Chinese, etc). I'm not sure how your application will be taking data (via the web, via a GUI toolkit, etc) but it's likely that whatever technology you're using supports unicode out of the box. That means that for any user-entered data (such as name) there is always the possibility of receiving non-Latin characters, if not now then in the future.

If I was building a new application, I would use nvarchar. Call it "future-proofing" if you like.

Todd Owen
A: 

on performance:
a reason to use varchar over nvarchar is that you can have twice as many characters in your indexes! index keys are limited to 900 bytes
on usability:
if the application is only ever intended for a english audience & contain english names, use varchar

Nick Kavadias
A: 

Data to store: "Sunil"

varchar(5) takes 7B nvarchar(5) takes 12B

Sunil Kumar Lalwani