Wednesday, 16 November 2016

CHAR VS VARCHAR

What are the differences between CHAR and VARCHAR [CHAR VS VARCHAR]?
Which data type should use for defining a column in a table (CHAR or VARCHAR)?


CHAR
VARCHAR
Bytes
It consumes 1 byte per character.
It consumes 1 byte per character.
Data Length
The maximum number of characters  for this data type can hold 8000 characters by defining as CHAR(8000).
The maximum number of characters for this data type can hold up to 8000 characters by defining as
VARCHAR(8000)  OR
 unlimited by defining as VARCHAR(MAX).

Data
Storage
Type
Used to store fixed length character value as String.
Used to store variable length value as String.
Storage
Content is a fixed size and not null, get better performance with CHAR. Content is lesser than defined size, then content filled with spaces that the content/data does not use up.
(store data with wasting the extra space)

Content is dynamic (Variable Length Size Variable) and null, manage storage space efficiently with VARCHAR. (store data without wasting the extra space)
Use When?
Use CHAR data type when content (data) is fixed size, does not have any UNICODE characters and NOT NULL.

Note:

Unicode Example:
¥
¼
Ñ
ñ
(Japanese,  Korean, etc.)
UNICODE exactly occupies
2 bytes.
Use VARCHAR data type when content (data) size is variable, does not have any UNICODE characters and / or  NULL.
Optional Parameter value
The optional parameter value N [CHAR(N)] is not specified in the variable declaration or column definition then it is considered as 1.

CHAR occupies 1 byte.

DECLARE @Char CHAR = 'Char'
PRINT DATALENGTH(@Char)

OUTPUT: 1

The optional parameter value N [VARCHAR(Nz)] is not specified in the variable declaration or column definition then it is considered as 1.



VARCHAR occupies 1 byte.

DECLARE @VarChar varchar = 'VarChar'
PRINT DATALENGTH(@VarChar)

OUTPUT: 1

Data Conversion
DECLARE @Name CHAR = 'Govindaraj Kanniappan'
SELECT CAST(@Name AS VARCHAR) Name,
DATALENGTH(CAST(@Name AS VARCHAR)) NameLength


OUTPUT :
Name
NameLength
G
1



DECLARE @Name VARCHAR = 'Govindaraj Kanniappan'

SELECT CAST(@Name AS CHAR) Name,
DATALENGTH(CAST(@Name AS CHAR)) NameLength

OUTPUT :
Name
NameLength
G
30
NOTE: VARCHAR default length is 30, while converting VARCHAR to CHAR, so the NameLength is 30 


Example
Use DATALENGTH()  - command would give you the difference of both types.

Example:

CREATE TABLE [dbo].[Test](
       [City] [char](15) NULL,
       [Street] [varchar](15) NULL
) ON [PRIMARY]
GO
INSERT [dbo].[Test] ([City], [Street]) VALUES (N'Delhi', N'Car Street')
GO

SELECT
DATALENGTH([City]) AS CharDataLength, DATALENGTH([Street]) AS VarcharDataLength
FROM [Test]

OUTPUT :
CharDataLength
VarcharDataLength
15
10

 Here, City content length is 5 but it is stored to table as 15 length with 10 extra spaces. Street content length is 10 it is stored as it is without wasting any spaces.

SELECT [City] + '|' AS City , [Street] + '|' AS Street
FROM [Test]

OUTPUT :
City
Street
Delhi__________|
Car Street|




No comments:

Post a Comment