In SQL Server, CHAR
, VARCHAR
, and NVARCHAR
are data types used to store character strings. However, they have different characteristics, storage requirements, and use cases. Here’s a detailed comparison of these data types:
1. CHAR
Definition:
CHAR
is a fixed-length character data type.
Storage:
- It always uses the defined length in bytes, regardless of the actual length of the string stored. For example, if you define a
CHAR(10)
and store a string of 5 characters, it will still use 10 bytes of storage, padding the remaining 5 bytes with spaces.
- It always uses the defined length in bytes, regardless of the actual length of the string stored. For example, if you define a
Use Case:
- Best used when the length of the data is consistent. For example, storing fixed-length codes like country codes or status codes.
Example:
sql1CREATE TABLE ExampleTable ( 2 FixedLengthColumn CHAR(10) 3);
2. VARCHAR
Definition:
VARCHAR
is a variable-length character data type.
Storage:
- It uses only the actual length of the string stored, plus 2 bytes for length information. For example, if you define a
VARCHAR(10)
and store a string of 5 characters, it will use 5 + 2 = 7 bytes of storage.
- It uses only the actual length of the string stored, plus 2 bytes for length information. For example, if you define a
Use Case:
- Best used when the length of the data varies significantly. For example, storing names, addresses, or any text where the length can change.
Example:
sql1CREATE TABLE ExampleTable ( 2 VariableLengthColumn VARCHAR(10) 3);
3. NVARCHAR
Definition:
NVARCHAR
is a variable-length Unicode character data type.
Storage:
- It uses 2 bytes per character, plus 2 bytes for length information. This allows it to store characters from multiple languages and special characters. For example, if you define an
NVARCHAR(10)
and store a string of 5 characters, it will use 5 * 2 + 2 = 12 bytes of storage.
- It uses 2 bytes per character, plus 2 bytes for length information. This allows it to store characters from multiple languages and special characters. For example, if you define an
Use Case:
- Best used when you need to store international characters or special symbols. For example, storing names in different languages or any text that requires Unicode support.
Example:
sql1CREATE TABLE ExampleTable ( 2 UnicodeColumn NVARCHAR(10) 3);
Key Differences
Feature | CHAR | VARCHAR | NVARCHAR |
---|---|---|---|
Length | Fixed-length | Variable-length | Variable-length |
Storage | Uses defined length in bytes | Uses actual length + 2 bytes | Uses 2 bytes per character + 2 bytes |
Character Set | Non-Unicode | Non-Unicode | Unicode |
Maximum Length | 8,000 characters | 8,000 characters | 4,000 characters (or 2^31-1 for NVARCHAR(MAX) ) |
Use Case | Fixed-length data | Variable-length data | International or special characters |
Conclusion
- Use
CHAR
when you know the data will always be of a fixed length. - Use
VARCHAR
when the length of the data can vary but does not require Unicode support. - Use
NVARCHAR
when you need to store text in multiple languages or special characters that require Unicode.
Choosing the appropriate data type is crucial for optimizing storage and ensuring that your database can handle the character data you need.
Surya Prakash (Admin) Reply
Hi Abhishek, Your feedback is valuable for Us . Thanks for giving your time for this blog.