Solved

Should I always use VARCHAR

Posted on 2011-02-15
6
483 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 50 total points
ID: 34895995
0
 
LVL 19

Assisted Solution

by:Bardobrave
Bardobrave earned 112 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 112 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
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 10

Accepted Solution

by:
John Claes earned 113 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 113 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

860 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