Solved

nchar(10) vs nvarchar(50)

Posted on 2010-08-29
7
2,099 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

777 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