Difference between CHAR and VARCHAR
The primary difference between the varchar and char types is data padding. If you have a column called FirstName that is a varchar(10) data type and you store the value of "SAM" in the column, only 3 bytes are physically stored, plus a little overhead. If you store the same value in a char(10) data type, all 10 bytes would be used. SQL inserts trailing spaces to fill the 10 characters.
Example depicting the difference between CHAR and VARCHAR
DECLARE @Var_Char Char(10) = 'SAM', @Var_Varchar VarChar(10) = 'SAM' SELECT DATALENGTH(@Var_Char) Char_Space_Used, DATALENGTH(@Var_Varchar) VarChar_Space_Used
Char_Space_Used | VarChar_Space_Used |
---|---|
10 | 3 |
Another good source of difference between CHAR and VARCHAR.
Also See:
Professional Microsoft SQL Server 2014 Administration by Adam Jorgensen, Bradley Ball, Steven Wort, Ross LoForte, Brian Knight