NCHAR data type is used to store Unicode string data of fixed-length.
- NCHAR data type is used to store Unicode string data of fixed-length.
- It stores data at 2 byte per character.
- It supports up to 4000 characters.
Syntax
nchar(n)
n specify the string length that ranges from 1 to 4000. Default value for n is 1.
Lets Look at an example using NCHAR data type in SQL Server.
Data Storage Vs Length
In following T- SQL Statement, declared a variable @str of datatype nchar of size 15.
DECLARE @Str NCHAR(15) SET @Str = 'Microsoft' SELECT @Str AS Strtext, DATALENGTH(@Str) AS StrText_OccupiedLength , LEN(@Str) AS StrText_length
As nchar has fixed-length and takes 2 bytes per character to store the data, so it takes 30 bytes to store value ‘microsoft’, even though the length of ‘microsoft’ is 9 .
So the data storage length of characters will be the fixed-length of variable @str * 2 bytes (15 *2) =30.
Using NCHAR to store a unicode data
NCHAR can store unicode data, Lets store the Hindi language text to @str variable of type nchar.
Note: Always make sure that, you prefix unicode string literals with an N prefix.
DECLARE @Str NCHAR(15) SET @Str = N'माइक्रोसॉफ्ट' SELECT @Str AS Strtext, DATALENGTH(@Str) AS StrText_OccupiedLength , LEN(@Str) AS StrText_length
You can see, it can store a unicode data as well that can be seen in strtext column if result set.
Also Read..
SQL Server VARCHAR VS NVARCHAR
1,699 total views, 1 views today
I find a small mistake
Hi Suresh A, we have revised a post, let us know if you still see any mistake or have any query.