SQL Server String Data Types
CHAR
CHAR is a fixed data type. So when a column is declared as CHAR, irrespective of the actual characters stored in that variable/column, it will use all the declared space.
In the above example you can see that even though the actual length of the variable is 6, It occupies 30 bytes of space.This is because when the variable @name is declared as CHAR(30), 30 bytes of space got reserved for the variable.
Also, you can observe that CHAR type column uses 1 byte of space to store each character.
Summary:
1. It is a fixed data type
2. Occupies 1 byte of space for each character
3. It is used to store non-unicode character.
VARCHAR
VARCHAR is a variable length data type. So when a variable or column is declared as VARCHAR, irrespective of declared space, it will use only space require to store all characters.
In the above example you can see that even though the declared size is 30, It occupied 6 bytes of space which is the actual length of stored string.
Also, you can observe that VARCHAR type column uses 1 byte of space to store each character
Summary:
1. It is a variable length data type
2. Occupies 1 byte of space for each character
3. It is used to store non-Unicode character.
NVARCHAR
Like VARCHAR it is also a variable length data type. But it uses 2 bytes of memory to store each character
In the above example you can see that even though the declared size is 30, It occupied 12 bytes of space which is double the actual length of stored string.
Summary:
1. It is a variable length data type
2. Occupies 2 byte of space for each character
3. It is used to store Unicode character.
Comments
Post a Comment