VARCHAR
, NVARCHAR
, and CHAR
are data types used to store character strings, but they have different characteristics and use cases. Below are the differences between these data types, along with examples for better understanding.
1. VARCHAR
- Definition:
VARCHAR
stands for Variable Character. It is used to store non-Unicode character data. - Storage: It uses 1 byte per character, plus 2 bytes for length information.
- Length: You can specify a maximum length (e.g.,
VARCHAR(50)
), and it can store up to 8,000 characters. - Use Case: Use
VARCHAR
when you are storing ASCII characters and you want to save space.
Example:
2. NVARCHAR
- Definition:
NVARCHAR
stands for National Variable Character. It is used to store Unicode character data. - Storage: It uses 2 bytes per character, plus 2 bytes for length information.
- Length: You can specify a maximum length (e.g.,
NVARCHAR(50)
), and it can store up to 4,000 characters (or up to 2 billion characters if you useNVARCHAR(MAX)
). - Use Case: Use
NVARCHAR
when you need to store characters from multiple languages or special characters (e.g., Chinese, Arabic).
Example:
3. CHAR
- Definition:
CHAR
stands for Character. It is used to store fixed-length non-Unicode character data. - Storage: It uses 1 byte per character.
- Length: You must specify a fixed length (e.g.,
CHAR(10)
), and it will always use that amount of space, padding with spaces if the actual string is shorter. - Use Case: Use
CHAR
when you know the exact length of the data you will store, such as fixed-length codes or identifiers.
Choosing the Right Data Type
- Use
VARCHAR
when you are dealing with standard ASCII characters and want to save space. - Use
NVARCHAR
when you need to support multiple languages or special characters. - Use
CHAR
when you have fixed-length data and want to ensure consistent storage size.
Let's try to understand ,determine the amount of memory each variable occupies.
SQL Code Example :
Explanation of Each Component
Variable Declarations:
DECLARE @Varchar VARCHAR(10) = 'Hello';
- This line declares a variable named
@Varchar
of typeVARCHAR(10)
and initializes it with the string'Hello'
. - The
VARCHAR
data type can store up to 10 characters, and it uses 1 byte per character.
- This line declares a variable named
DECLARE @Nvarchar NVARCHAR(10) = 'Hello';
- This line declares a variable named
@Nvarchar
of typeNVARCHAR(10)
and initializes it with the same string'Hello'
. - The
NVARCHAR
data type can also store up to 10 characters, but it uses 2 bytes per character because it is designed to store Unicode characters.
- This line declares a variable named
DECLARE @Char CHAR(10) = 'Hello';
- This line declares a variable named
@Char
of typeCHAR(10)
and initializes it with the string'Hello'
. - The
CHAR
data type is fixed-length, meaning it will always occupy the full length specified (10 characters in this case). If the string is shorter than 10 characters, it will be padded with spaces.
- This line declares a variable named
Using DATALENGTH Function:
SELECT DATALENGTH(@Varchar) AS [Used Memory for Varchar];
- This query calculates the number of bytes used to store the value of
@Varchar
using theDATALENGTH
function. - Since
'Hello'
has 5 characters, the memory used will be 5 bytes.
- This query calculates the number of bytes used to store the value of
SELECT DATALENGTH(@Nvarchar) AS [Used Memory for NVarchar];
- This query calculates the number of bytes used to store the value of
@Nvarchar
. - The string
'Hello'
consists of 5 characters, and sinceNVARCHAR
uses 2 bytes per character, the memory used will be 5 * 2 = 10 bytes.
- This query calculates the number of bytes used to store the value of
SELECT DATALENGTH(@Char) AS [Used Memory for Char];
- This query calculates the number of bytes used to store the value of
@Char
. - The
CHAR(10)
data type is fixed-length, so it will occupy 10 bytes regardless of the actual length of the string. The string'Hello'
is 5 characters long, so it will be padded with 5 spaces, resulting in a total of 10 bytes.
- This query calculates the number of bytes used to store the value of
Summary of Memory Usage
Used Memory for Varchar
:- Value: 5 bytes
- Explanation: The
VARCHAR
data type uses 1 byte per character. The string'Hello'
has 5 characters, so it occupies 5 bytes.
Used Memory for NVarchar
:- Value: 10 bytes
- Explanation: The
NVARCHAR
data type uses 2 bytes per character. The string'Hello'
has 5 characters, so it occupies 5 * 2 = 10 bytes.
Used Memory for Char
:- Value: 10 bytes
- Explanation: The
CHAR(10)
data type is fixed-length. The string'Hello'
is 5 characters long, but since it is defined asCHAR(10)
, it occupies the full 10 bytes, padded with 5 spaces.