Open In App

VARCHAR, VARCHAR(MAX), and NVARCHAR in MS SQL Server

Last Updated : 12 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In SQL Server, VARCHAR, VARCHAR(MAX) and NVARCHAR are used to store variable-length text data. VARCHAR is efficient for non-Unicode text up to 8,000 characters, while VARCHAR(MAX) handling larger text data up to 2 GB. NVARCHAR supports Unicode data, making it suitable for multilingual applications.

In this article, We will discuss the VARCHAR, VARCHAR (MAX) and NVARCHAR datatypes with the help of examples in SQL Server.

VARCHAR in MS SQL Server

  • Variable Character or VARCHAR for short, is a data type that stores non-Unicode data.
  • We should use VARCHAR when the sizes of the column data entries vary considerably.
  • It only stores the actual length of the data. If n is not specified, the default length is 1 character.

Syntax

VARCHAR (n)

Here, n is the number of bytes. The maximum storage capacity is up to 8000 bytes. If n is not specified, the default length is 1 character.

VARCHAR(MAX) Datatype in MS SQL Server

  • It stores character string data with a maximum storage size of 2³¹-1 bytes.
  • It can store up to 2 GB of data, which is significantly larger than the VARCHAR(n) data type.

Syntax

VARCHAR(max)

The NVARCHAR datatype

This stores variable length unicode data. NVARCHAR is used when there is variability in the length of the data, such as storing names in different languages.

Syntax

NVARCHAR(n)

Here n is the number of bytes and can store upto 4000 bytes. If the length for the datatype isn't specified, it takes the default value of 1.

Difference Between VARCHAR, VARCHAR(MAX) AND NVARCHAR

Let’s learn the differences between VARCHAR, VARCHAR(MAX) and NVARCHAR. We’ll see what makes each one unique, how they’re different from each other, and why we might use one over the other.

It Does not support Unicode

ParameterVARCHARVARCHAR(MAX)NVARCHAR
DefinitionVariable-length character data typeVariable-length character data type with a maximum size of 2 GBVariable-length Unicode character data type
StorageStores up to 8,000 charactersStores up to 2 GB of charactersStores up to 4,000 characters (or more with NVARCHAR(MAX))
Length SpecificationLength specified is the maximum possible sizeLength can be up to 2 GBLength specified is the maximum possible size (or up to 2 GB with NVARCHAR(MAX))
Storage EfficiencyEfficient for varying-length data up to 8,000 charactersEfficient for very large text dataEfficient for varying-length Unicode data
PerformanceGenerally efficient for text up to 8,000 charactersSuitable for very large text but may incur overhead for large sizesGenerally efficient for Unicode data up to 4,000 characters (or more)
Use CaseText data where maximum length is known and is less than 8,000 charactersLarge text data or very large stringsUnicode text data that requires support for multiple languages
Unicode SupportIt Does not support UnicodeIt Supports Unicode
ExampleVARCHAR(50) allows storing up to 50 charactersVARCHAR(MAX) allows storing very large textNVARCHAR(50) allows storing up to 50 Unicode characters
SQL Query ExampleCREATE TABLE Products (ProductDescription VARCHAR(50))CREATE TABLE Documents (Content VARCHAR(MAX))CREATE TABLE Users (Name NVARCHAR(100))

Conclusion

Overall, Selecting between VARCHAR, VARCHAR(MAX), and NVARCHAR depends on your text data requirements. Use VARCHAR for moderate, non-Unicode text, VARCHAR(MAX) for large text needs, and NVARCHAR for Unicode text to ensure efficient storage and optimal performance.


Next Article
Article Tags :

Similar Reads