61
Views
2
Comments
What is the difference between a text length of 50 and 2000?
Question

When you set the text length of an attribute greater than 2000, the text will be stored as NVARCHAR(MAX) in a SQL Server Database. This can impact performance and database storage. However, I would like to understand the difference between setting the length to 50 characters versus 2000 characters. For example, consider a scenario where a text attribute is configured with a length of 2000 characters, but all the records contain fewer than 100 characters. In such cases, does the database still allocate 2000 characters of space for each record? Does fetching the data take longer? If not, why wouldn't you always set the text lenght to 2000? 

2024-02-27 12-57-22
Sam Rijkers

Some tests were done in this article: https://stackoverflow.com/questions/8295131/best-practices-for-sql-varchar-column-length

Note that it is an old thread, I am not sure if this performance difference still holds. Feel free to dive in the documentation yourself.

Besides, there are some other arguments to define the attribute length with a character limit fewer than 2000. For example:
- When scaffolding an input using the database attribute automatically sets the max-length to the right value. Just like the Mandatory property does not do anything on a database level for text attributes. 
- It makes understanding the code much easier when reviewed by another developer, or by yourself later. It will make it easier to understand what type of data to expect.
- It helps finding bugs in an earlier stage, should you receive a value longer than the length that you expect.

There are probably more reasons to think of.

2024-07-05 14-16-55
Daniël Kuhlmann
 
MVP
Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.