In SQL Server 2000, the most data that could be stored in a varchar, nvarchar, or varbinary
column was 8,000 bytes. If you needed to store a larger value in a
single column, you had to use the large object (LOB) data types: text, ntext, or image. The main disadvantage of using the LOB data types is that they cannot be used in many places where varchar or varbinary data types can be used (for example, as local variables, as arguments to SQL Server string manipulation functions such as REPLACE, and in string concatenation operations).
SQL Server 2005 introduced the max specifier for varchar and varbinary data types. This specifier expands the storage capabilities of the varchar and varbinary data types to store up to 231-1 bytes of data, which is the same maximum size of text and image data types. The main difference is that these large value data types can be used just like the smaller varchar, nvarchar, and varbinary data types. The large value data types can be used in functions where LOB objects cannot (such as the REPLACE function), as data types for Transact-SQL variables, and in string concatenation operations. They can also be used in the DISTINCT, ORDER BY, and GROUP BY clauses of a SELECT statement as well as in aggregates, joins, and subqueries.
The following example shows a local variable being defined using the varchar(max) data type:
declare @maxvar varchar(max)
go
However, a similar variable cannot be defined using the text data type:
declare @textvar text
go
Msg 2739, Level 16, State 1, Line 2
The text, ntext, and image data types are invalid for local variables.declare
@maxvar varchar(max)
The remaining examples in this section make use of the following table to demonstrate the differences between a varchar(max) column and text column:
create table maxtest (maxcol varchar(max),
textcol text)
go
-- populate the columns with some sample data
insert maxtest
select replicate('1234567890', 1000), replicate('1234567890', 1000)
go
In the following example, you can see that the substring function works with both varchar(max) and text data types:
select substring (maxcol, 1, 10),
substring (textcol, 1, 10)
from maxtest
go
maxcol textcol
---------- ----------
1234567890 1234567890
However, in this example, you can see that while a varchar(max) column can be used for string concatenation, the text data type cannot:
select substring('xxx' + maxcol, 1, 10) from maxtest
go
----------
xxx1234567
select substring('xxx' + textcol, 1, 10) from maxtest
go
Msg 402, Level 16, State 1, Line 1
The data types varchar and text are incompatible in the add operator.
With the introduction of the max
specifier, the large value data types are able to store data with the
same maximum size as the LOB data types, but with the ability to be used
just as their smaller varchar, nvarchar, and varbinary counterparts. It is recommended that the max
data types be used instead of the LOB data types because the LOB data
types will be deprecated in future releases of SQL Server.