Solved

nchar(10) vs nvarchar(50)

Posted on 2010-08-29
7
1,979 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

708 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

11 Experts available now in Live!

Get 1:1 Help Now