?
Solved

nchar(10) vs nvarchar(50)

Posted on 2010-08-29
7
Medium Priority
?
2,734 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 800 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 57

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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 57

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 57

Assisted Solution

by:HainKurt
HainKurt earned 400 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 57

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Suggested Courses

770 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