String Data Types in SQL Server

In sql, string data types are used to store any kind of data in table. In string data types, we have an option to allow users to store either 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 has contain 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 a 50 characters of data. In case, if we insert only 10 characters of 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 a non-unicode characters. It will allocate the memory based on number characters inserted. Suppose if we declare a varchar(50), then it will allocate a memory of 0 characters at the time of declaration. Now if we insert only 10 characters of string, then it will allocate memory for only 10 characters.

 

Please check following table for more details regarding character string data types in sql server.

 

Data TypeDescriptionStorage
char It’s fixed length character string and we can store maximum 8000 characters Based on defined width
varchar(n) It’s variable length character string and we can store maximum 8000 characters 2 Bytes number of characters
varchar(max) It’s 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 a huge data. In Unicode character string, we have a different type of string data types available, those are

 

  • nchar
  • nvarchar
  • nvarchar(max)

Please check following table for more details regarding Unicode character string data types in sql server.

 

Data TypeDescription
nchar It’s fixed length character string and we can store maximum 4000 characters
nvarchar(n) It’s 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)

Topics Covered