为什么使用CAST时VARCHAR的默认长度为30? [英] Why is 30 the default length for VARCHAR when using CAST?

查看:604
本文介绍了为什么使用CAST时VARCHAR的默认长度为30?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SQL Server 2005中,此查询

In SQL server 2005 this query

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

返回30作为长度,而提供的字符串有更多字符。这似乎是默认。为什么30,而不是32或任何其他权力2?

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?

我知道我应该总是指定长度到varchar,但这是一个快速让检查的东西查询。问题仍然存在,为什么30?

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

推荐答案

为什么不指定varchar长度?即:

Why don't you specify the varchar length? ie:

SELECT CAST('the quick brown fox jumped over the lazy dog' AS VARCHAR(45))

至于为什么30,那是该类型的SQL服务器的默认长度

As far as why 30, thats the default length in SQL server for that type

char和varchar(Transact-SQL)


当在数据定义或变量声明语句中未指定n时,默认长度为1. 使用CAST和CONVERT函数时未指定,默认长度为30.

这篇关于为什么使用CAST时VARCHAR的默认长度为30?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆