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

后端 未结 6 585
误落风尘
误落风尘 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:17

    Default size with convert/cast has nothing to do with the memory allocation and hence the default value (ie 30) is not related to any power of 2.

    regarding why 30, this is microsoft's guideline which gives this default value so as to cover the basic data in first 30 characters. http://msdn.microsoft.com/en-us/library/ms176089.aspx

    Although one can always alter the length during conversion/cast process

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

提交回复
热议问题