Solved

Should I always use VARCHAR

Posted on 2011-02-15
6
478 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
Comment Utility
0
 
LVL 19

Assisted Solution

by:Bardobrave
Bardobrave earned 112 total points
Comment Utility
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 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 112 total points
Comment Utility
>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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Accepted Solution

by:
John Claes earned 113 total points
Comment Utility
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
Comment Utility
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
Comment Utility
thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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, show how to extract information from SQL Server on Database, Connection and Server properties

771 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now