PDA

View Full Version : SQL Server varchar(x) Purpose


expinch
12-19-2007, 04:17 PM
From all of the documentation that I've read, it seems that database columns of type varchar only allocate enough space to store the designated characters (plus 2 types of overhead). So, if we have data that takes 33 characters, sql server would only need 35 characters to store that data.

So, what is the purpose of providing a value in the parenthesis after the varchar: varchar(32)? What does the 32 here provide?

I've always thought this number represented the largest possible length of characters, and this seems to be the case when I do testing. But why provide a number to begin with? If the server will only allocate the space needed for the data, why not just use varchar(max) just in case the data may grow one day?

Is it only provided for integrity purposes? If you absolutely don't want this data to exceed 32 characters?

Also, does this mean that you should ALWAYS use varchar(max) when you don't know what the length will be?

scoutt
12-24-2007, 05:29 PM
That is correct. You supply a value if you do not want the data to go beyond that.