Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DATALENGTH VARCHAR in SQL Server

According to MSDN the storage size of a VARCHAR data type is the actual length of the data entered + 2 bytes. I'm trying to understand why when I run this query:

DECLARE @VARCHAR VARCHAR(40) = 'RACING CLUB DE AVELLANEDA'
SELECT DATALENGTH(@VARCHAR) AS Bytes, LEN(@VARCHAR) AS Characters;

The storage size of @VARCHAR is the same as it was a CHAR data type, It's 25 and not 27 (25+2).

Does it has something to do with DATALENGTH function?

like image 712
Hernan Demczuk Avatar asked Nov 29 '25 12:11

Hernan Demczuk


1 Answers

datalength will tell you the number of bytes of data, which doesn't include the 2 bytes that precede that data in storage. The 2 byte overhead of a varchar is for recording the number of bytes in the string - i.e. in your case 25

like image 174
Scott C Avatar answered Dec 02 '25 02:12

Scott C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!