CHAR and VARCHAR page 3 |
|||||||||||||||||
Let's consider the following code.
Here we define the simple variable of VARCHAR(2) data type and the table (table variable) with the single column of the same data type - VARCHAR(2). Running this code results in truncation of variable value up to defined size, whereas the error arises, when inserting the row in the table, due to the column undersize: String or binary data would be truncated. This explains the result obtained - NULL in the col column, i.e. data does not be inserted into the table. Regarding character data, it should be made a remark about its available sizes.
This example shows us that simple character data types are truncated up to SQL Server data page - 8000 bytes (NVARCHAR type uses 2 bytes per symbol). VARCHAR(MAX) data type allows to store strings up to 2Gb. It should be taken into account particularly when concatenating strings with beforehand unknown size of resulting string, which could result in wrong analysis:
Above is concatenation of two strings of 7000 symbols each. As a result we get string of 8000 symbols in size, the exceed symbols are truncated without error message. |