- Use varchar when the sizes of the column data entries vary
considerably. - Use varchar(max) when the sizes of the column data entries vary
considerably, and the size might exceed 8,000 bytes.
When the the length is specified in declaring a VARCHAR
variable or column, the maximum length allowed is 8000. If the length is greater than 8000, you have to use the MAX
specifier as the length. If a length greater than 8000 is specified, the following error will be encountered (assuming that the length specified is 10000):
The size (10000) given to the type ‘varchar’ exceeds the maximum allowed for any data type (8000).
UPDATE :-
I found a link which I would like to share:-
There is not much performance difference between Varchar[(n)]
and Varchar(Max)
. Varchar[(n)]
provides better performance results compared to Varchar(Max)
. If we know that data to be stored in the column or variable is less than or equal to 8000 characters, then using this Varchar[(n)] data type provides better performance compared to Varchar(Max).Example: When I ran the below script by changing the variable @FirstName
type to Varchar(Max)
then for 1 million assignments it is consistently taking double time than when we used data type as Varchar(50)
for variable @FirstName
.
DECLARE @FirstName VARCHAR(50), @COUNT INT=0, @StartTime DATETIME = GETDATE()
WHILE(@COUNT < 1000000)
BEGIN
SELECT @FirstName="Suraj", @COUNT = @COUNT +1
END
SELECT DATEDIFF(ms,@StartTime,GETDATE()) 'Time Taken in ms'
GO