在 SQL Server 2005 中此查询
select len(cast('the quick brown fox jumped over the lazy dog' as varchar))
当提供的字符串有更多字符时,返回 30 作为长度。这似乎是默认的。为什么是 30,而不是 32 或任何其他 2 的幂?
[编辑] 我知道在转换为 varchar 时我应该始终指定长度,但这是一个快速的让我们检查某些内容的查询。问题仍然存在,为什么是 30?
最佳答案
为什么不指定 varchar 长度?即:
SELECT CAST('the quick brown fox jumped over the lazy dog' AS VARCHAR(45))
至于为什么是 30,这是 SQL Server 中该类型的默认长度。
来自char and varchar (Transact-SQL) :
When n is not specified in a data definition or variable declaration statement, the default length is 1. When n is not specified when using the CAST and CONVERT functions, the default length is 30.
关于sql - 为什么使用 CAST 时 VARCHAR 的默认长度为 30?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/359257/