Alex Rivera | Logout

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

Asked 2008-12-11T12:58:03.940
66

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 supplied string has more characters. This seems to be the default. Why 30, and not 32 or any other power of 2?

[EDIT] I am aware that I should always specifiy the length when casting to varchar but this was a quick let's-check-something query. Questions remains, why 30?

Edit
Report

1 Answer

1

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)))
answered 2012-07-17T19:18:40.073

Your Answer