Wednesday, 30 November 2016

CHAR VS NCHAR

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


CHAR
NCHAR
Bytes
It consumes 1 byte per character.

It consumes 2 bytes 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 4000 characters by defining as NCHAR(4000).
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, does not have any UNICODE and not null, get better performance with CHAR. Content is lesser than defined size, then content filled with spaces. (store data with wasting the extra space)

Content is a fixed size, has any UNICODE characters and NOT NULL, then content filled with spaces. (store data with 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 NCHAR data type when content (data) size is variable, has 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  [NCHAR(N)] is not specified in the variable declaration or column definition then it is considered as 1.

NCHAR occupies 2 bytes.

DECLARE @NChar NCHAR = 'NChar'
PRINT DATALENGTH(@NChar)

OUTPUT: 2

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 NCHAR) Name,
DATALENGTH(CAST(@Name AS NCHAR)) NameLength

OUTPUT :
Name
NameLength
G
60

NOTE: VARCHAR default length is 30, while converting VARCHAR to NCHAR, so the NameLength is 60 


Example
Use DATALENGTH()  - command would give you the difference of both types by changing the data type FROM CHAR to NCHAR.

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