Solved

nchar(10) vs nvarchar(50)

Posted on 2010-08-29
7
2,500 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 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 55

Expert Comment

by:Huseyin KAHRAMAN
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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 55

Expert Comment

by:Huseyin KAHRAMAN
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 55

Assisted Solution

by:Huseyin KAHRAMAN
Huseyin KAHRAMAN 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 55

Expert Comment

by:Huseyin KAHRAMAN
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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

705 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