Solved

nchar(10) vs nvarchar(50)

Posted on 2010-08-29
7
2,032 Views
Last Modified: 2012-05-10
Can someone explain in laymens terms what is the difference between these two data types?

nchar(10) vs nvarchar(50)
0
Comment
Question by:al4629740
  • 4
  • 2
7 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 200 total points
ID: 33555440
NChar/NVarchar is used to store uNicode/iNternational data, using 2 bytes to encode each character.

Why 2 bytes / what is unicode?  http://en.wikipedia.org/wiki/Unicode

Char vs Varchar

Char explicitly stores spaces into the string to make up to the desired length.
So for nchar(10), the string "bob" is stored as "bob       " (7 trailing spaces)

nvarchar(50) is a column that can store up to 50 characters (using up to 100 bytes), but it can store any number of characters less than 50 as well without adding trailing spaces.
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 33555445
cyberkiwi left nothing for us to grab some points ;)
0
 

Author Comment

by:al4629740
ID: 33555449
So does nchar(10) pose some problems when trying to query data vs. nvarchar(50)
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 51

Expert Comment

by:HainKurt
ID: 33555458
i never use nchar(x), use nvarchar(x) all the time, if necessary use sql functions to pad it to desired level
0
 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 100 total points
ID: 33555468
for example if "code" is nvarchar(10) and I want 10 char all the time in my some reports, I can use

select left(code + "          ", 10) as padded_code ....
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 33555471
it will be tricky to write your queries with nchar(x)
you may need to use RTrim(column) all the time....
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33555505
> So does nchar(10) pose some problems when trying to query data vs. nvarchar(50)

It may, due to the trailing spaces.  If you have a nchar(10) column and you store "Bob" into it, and you add it to a firstname, like Col1 + ", Jones", you won't get "Bob, Jones", you get "Bob       , Jones".


HainKurt FYI,

http://msdn.microsoft.com/en-us/library/ms186939.aspx

The storage size, in bytes, is two times the number of characters entered + 2 bytes.

If the customer code is always going to be 10 characters, nchar(10) will save you 2 bytes per row compared to nvarchar(10).

Having it as VAR(ying) length requires 2 additional bytes to store the actual length of data.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now