Home » SQL Server CHAR

SQL Server CHAR

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 to demonistarte CHAR data type.

Data Storage Vs Length

Here we have a variable @strtxt 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 @strtxt value ‘microsoft’  ,even though the length of @Strtxt ‘microsoft’  is 9 .

So the  data storage lenght of characters will be the fixed-lenght of variable @strtxt * 1 bytes (15 *1) =15.

Can store Unicode Data ?

Char can not store unicode data , here we try to store hindi language text to @str variable of type char.

DECLARE @Str CHAR(15)

SET @Str = N'माइक्रोसॉफ्ट'

SELECT @Str AS Strtext, DATALENGTH(@Str) AS StrText_OccupiedLength ,
LEN(@Str) AS StrText_length

 

Recommended

SQL Server VARCHAR VS NVARCHAR

SQL Server CHAR VS NCHAR

How to get the detailed information of all datatypes in SQL Server ?




Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.