String Data Types in SQL Server

In sql, string data types are used to store any kind of data in the table. In string data types, we have an option to allow users to store either the fixed length of characters or huge length data based on their requirements.

 

In SQL we have a different type of string data types available, those are

 

  • Character String Data Types
  • Unicode Character String Data Types

SQL Character String Data Types

In sql, the character string data types have contained a different type of character data types, those are 

 

  • char datatype
  • varchar datatype

SQL Char DataType

In sql, char datatype is used to store a fixed length of characters. Suppose if we declare a char(50), then it will allocate a memory for 50 characters to hold 50 characters of data. In case, if we insert only 10 characters of a string, then only 10 characters of memory will be used and remaining 40 characters of memory will be wasted.

SQL Varchar DataType

In sql, varchar means variable characters and it is used to store non-unicode characters. It will allocate the memory based on the number characters inserted. Suppose if we declare varchar(50), then it will allocate memory of 0 characters at the time of declaration. Now if we insert only 10 characters of a string, then it will allocate memory for only 10 characters.

 

Please check the following table for more details regarding character string data types in SQL Server.

 

Data TypeDescriptionStorage
char It’s a fixed-length character string and we can store maximum 8000 characters Based on defined width
varchar(n) It’s a variable-length character string and we can store maximum 8000 characters 2 Bytes number of characters
varchar(max) It’s a variable-length character string and we can store maximum 2^31-1 characters (upto 2 GB) 2 Bytes number of characters

SQL Unicode Character String Data Types

In sql, Unicode character string data types are used in a situation where we required to store huge data. In Unicode character string, we have a different type of string data types available, those are

 

  • nchar
  • nvarchar
  • nvarchar(max)

Please check the following table for more details regarding Unicode character string data types in SQL Server.

 

Data TypeDescription
nchar It’s a fixed-length character string and we can store maximum 4000 characters
nvarchar(n) It’s a variable-length character string and we can store maximum 4000 characters
nvarchar(max) It’s variable-length character string and we can store maximum 2^30-1 characters (upto 2 GB)