40 character string: will there be a difference if I use VARCHAR (4000) instead of VARCHAR (40)?

In my SP, I need to work with a string that can contain up to 40 characters.

Will it make a difference if I use VARCHAR(4000)instead VARCHAR(40)?

Update:

The reason why I ask this question lies in the fact that, as far as I know, the memory usage of the variable VARCHAR(4000)and VARCHAR(40)the same. I am trying to understand why I should limit myself to a variable VARCHAR(40)? Why not VARCHAR(4000)? Is it because performance or memory efficiency?

Update 2, Important Assumption:

I assume that when we store the string "Hello World" in a variable of any type VARCHAR(4000)or VARCHAR(40), it takes the same amount of memory. I'm right?

+4
source share
1 answer

Declaring a variable like VARCHAR(40)or VARCHAR(4000)behaves the same at runtime in terms of performance or memory consumption. But there is an important difference in behavior: it VARCHAR(40)can silently truncate a value to 40 lengths. This is why it is best used VARCHAR(8000)for variables (or NVARCHAR(4000)) to avoid the risk of silent truncation. Do not use VARCHAR(MAX)if value is not equal MAX, because types MAXbehave differently at runtime.

+4
source

Source: https://habr.com/ru/post/1651990/


All Articles