Solved

Should I always use VARCHAR

Posted on 2011-02-15
6
482 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 142

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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

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