Monday, June 6, 2011

SQL : char, nchar, nchar, nvarchar

  • nchar and nvarchar can store Unicode characters.
  • char and varchar cannot store Unicode characters.
  • char and nchar are fixed-length which will reserve storage space for number of characters you specify even if you don't use up all that space.
  • varchar and nvarchar are variable-length which will only use up spaces for the characters you store. It will not reserve storage like char or nchar.
nchar and nvarchar will take up twice as much storage space, so it may be wise to use them only if you need Unicode support.

You can use char when the data entries in a column are expected to be the same size.
You can use varchar when the data entries in a column are expected to vary considerably in size.

Easy way to Remember
n - Can store Unicode characters
var - variable length

DECLARE @myVariable AS char(40)
SET @myVariable = 'Prasad'
SELECT DATALENGTH(@myVariable)

Answer is 40

=============================================

DECLARE @myVariable AS varchar(40)
SET @myVariable = 'Prasad'
SELECT DATALENGTH(@myVariable)

Answer is 6



To know more about UNICODE go thru Wikipedia or this article
To
know more about other Data Types read this blog
A ver well written FAQ's on SQL .Must Read

0 comments:

Post a Comment