question

m.chithara avatar image
m.chithara asked

Usage of "n" in VARCHAR(n)

Hi, I would like to understand the logic of using "n" in VARCHAR(n). As per my understanding, VARCHAR will occupy the Insert data space only. For Example: For VARCHAR(100), If the Insert data value is "Data", the space occupied is 4, Irrespective of the length specified for "n". Whether n = 100 or n = 200, there is no difference in space occupation. Then What is the need of "n"? Can anyone please explain this. Thanks
sqlvarchar
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

· Write an Answer
Tom Staab avatar image
Tom Staab answered
The number specifies the maximum size allowed and can be no greater than 8000. Smaller values are helpful to UI developers and also matter greatly for indexes. For example, you might want to index a 2 character code or even a 20 character name, but you can't index a 2000 character string. In fact, the total combined length of all key columns in any clustered or nonclustered index is 900 bytes. Also, when declaring variables, you must specify the length or else it will default to 1 character.
1 comment
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Hi Tom Staab, Thank you so much for the response. I have one more doubt. Will there be any performance difference based on "n". For example: i have a table with 100 columns, dont have index in it and most of the columns(say 60) are VARCHAR(8000) means, will that affect performance? Note: I know Heap table will have performance issue, But i want to know will there be performance issue based on "n" value. Thanks, Maria
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.