Why is 30 the default length for VARCHAR when using CAST?

后端 未结 6 588
误落风尘
误落风尘 2020-12-03 16:38

In SQL server 2005 this query

select len(cast(\'the quick brown fox jumped over the lazy dog\' as varchar))

returns 30 as length while the

6条回答
  •  难免孤独
    2020-12-03 17:26

    On your question as to why 30 and not 32 or any other power of 2, the storage size is n + 2 bytes for varchar(n), which makes the byte storage size 32 for a string of length 30. Might be that this is what they looked at?

    Then just a point of clarity on some of the comments: The default length for an unspecified length varchar field is n=1. The default string length that CAST or CONVERT returns for a conversion of this data type is 30.

    Very cool question!

提交回复
热议问题