VARCHARNVARCHAR, 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

  • DefinitionVARCHAR 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:

sql
1DECLARE @exampleVarchar VARCHAR(50); 2SET @exampleVarchar = 'Hello, World!'; 3SELECT @exampleVarchar AS ExampleVarchar;

2. NVARCHAR

  • DefinitionNVARCHAR 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 use NVARCHAR(MAX)).
  • Use Case: Use NVARCHAR when you need to store characters from multiple languages or special characters (e.g., Chinese, Arabic).

Example:

sql
1DECLARE @exampleNvarchar NVARCHAR(50); 2SET @exampleNvarchar = N'??,??!'; -- Chinese for "Hello, World!" 3SELECT @exampleNvarchar AS ExampleNvarchar;

3. CHAR

  • DefinitionCHAR 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 :

sql
1DECLARE @Varchar VARCHAR(10) = 'Hello'; 2DECLARE @Nvarchar NVARCHAR(10) = 'Hello'; 3DECLARE @Char CHAR(10) = 'Hello'; 4 5SELECT DATALENGTH(@Varchar) AS [Used Memory for Varchar]; 6SELECT DATALENGTH(@Nvarchar) AS [Used Memory for NVarchar]; 7SELECT DATALENGTH(@Char) AS [Used Memory for Char];

Explanation of Each Component

  1. Variable Declarations:

    • DECLARE @Varchar VARCHAR(10) = 'Hello';
      • This line declares a variable named @Varchar of type VARCHAR(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.
    • DECLARE @Nvarchar NVARCHAR(10) = 'Hello';
      • This line declares a variable named @Nvarchar of type NVARCHAR(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.
    • DECLARE @Char CHAR(10) = 'Hello';
      • This line declares a variable named @Char of type CHAR(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.
  2. 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 the DATALENGTH function.
      • Since 'Hello' has 5 characters, the memory used will be 5 bytes.
    • 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 since NVARCHAR uses 2 bytes per character, the memory used will be 5 * 2 = 10 bytes.
    • 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.

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 as CHAR(10), it occupies the full 10 bytes, padded with 5 spaces.

Now , we hope you understood the differences among varchar, nvarchar and char . 

Leave a Reply

Your email address will not be published. Required fields are marked *


Talk to us?

Post your blog

F.A.Q

Frequently Asked Questions