Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Should I always use VARCHAR

Posted on 2011-02-15
6
Medium Priority
?
501 Views
Last Modified: 2012-05-11

VARCHAR requires 1 bytes per character

NVARCHAR requires 2 bytes per character

Should I always use VARCHAR
0
Comment
Question by:Mr_Shaw
6 Comments
 
LVL 6

Assisted Solution

by:loopfinity
loopfinity earned 200 total points
ID: 34895995
0
 
LVL 19

Assisted Solution

by:Bardobrave
Bardobrave earned 448 total points
ID: 34896180
If I'm not mistaken, varchar will reserve the whole space for your field, while nvarchar will reserve space dinamically.

So, if you have a varchar(50), it will ocupy always 50 bytes, while a nvarchar(50) will ocupy 10 bytes when you store a record with 5 chars and 20 bytes when you store a record with 10 chars.

varchar will reserve space even for empty (and maybe null, but I'm not really sure) records. So, in the long term, usually nvarchar will allow you to save space except on fields where you are very aware of the chars that is used (IDs, phones, etc...).

So, for strictly inputted data use varchar, and for free text use nvarchar.
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 448 total points
ID: 34896209
>If I'm not mistaken, varchar will reserve the whole space for your field, while nvarchar will reserve space dinamically.
you confuse CHAR vs VARCHAR

CHAR/NCHAR will indeed reserve the space (using trailing spaces)
VARCHAR/NVARCHAR will not reserve space, but only use space as needed.

both will NOT reserve any space if NULL is to be stored.

apart from that, indeed the NCHAR and NVARCHAR will use 2 bytes per character, to allow multi-codepage data to be stored.
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 10

Accepted Solution

by:
John Claes earned 452 total points
ID: 34896460
Mr_Shaw:

the answer for your question is One we can't answer.

We only can tell you the difference and you must decide then what you need/don't need.

char indeed reserves the spaces (1byte / Char)
varchar doesn't reserve the space but will use dynamicaly (1byte / Char)

the nChar and nVarchar are the MultiCode page versions of the 2 above.
For this expansion they indeed need 2 bytes for each char

So Now you need to decide do you need multiCode text to be saved in the database?
Yes : Use the N-Versions
No : Use the normal versions


regards

poor beggar
0
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 452 total points
ID: 34899018
VARCHAR and NVARCHAR require an extra 2 bytes to store the string size.  So a 15 character string will require 17 bytes for a VARCHAR and 32 bytes for a NVARCHAR, against 15 bytes for CHAR and 30 bytes for NCHAR.

So to save space I would recommend CHAR/NCHAR for columns that hold attributes that are truly fixed in length (like most codes) or have only 1 or 2 characters (CHAR(1), CHAR(2), NCHAR(1), NCHAR(2)).
0
 

Author Closing Comment

by:Mr_Shaw
ID: 34899043
thanks
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

885 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