SQL Server CHAR data type is used to store non-Unicode string data of fixed-length.
- CHAR data type is used to store non-Unicode string data of fixed-length.
- Stores data at 1 byte per character.
- Supports up to 8000 characters.
Syntax
char(n)
n specify the string length that ranges from 1 to 8000. Default value for n is 1.
Lets Look at an example of using CHAR data type in SQL Server.
Data Storage Vs Length
In following T-SQL statement, declared a variable @Str of datatype char of size 15.
DECLARE @Str CHAR(15) SET @Str = 'Microsoft' SELECT @Str AS Strtext, DATALENGTH(@Str) AS StrText_OccupiedLength, LEN(@Str) AS StrText_length
As char has fixed-length and takes 1 bytes per character to store the data so it takes 15 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 @strtxt * 1 bytes (15 *1) =15.
Using Char to store Unicode Dataย
Char can not store Unicode data. Lets try to store the Unicode data to Char.
In following T-SQL, we are storing a Hindi language text to @str variable that is of type char.
DECLARE @Str CHAR(15) SET @Str = N'เคฎเคพเคเคเฅเคฐเฅเคธเฅเคซเฅเค' SELECT @Str AS Strtext, DATALENGTH(@Str) AS StrText_OccupiedLength , LEN(@Str) AS StrText_length
You can see, it does not support the Unicode data that can be seen in the first column Strtext of result set.
Also Read..
SQL Server VARCHAR VS NVARCHAR
How to get the detailed information of all datatypes in SQL Server ?