Why is 30 the default length for VARCHAR when using CAST?(为什么使用 CAST 时 VARCHAR 的默认长度是 30?)
问题描述
在 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('敏捷的棕色狐狸跳过懒狗' AS VARCHAR(45))至于为什么 30,这是 SQL Server 中该类型的默认长度.
来自 char 和 varchar (Transact-SQL):><块引用>
在数据定义或变量声明语句中未指定n时,默认长度为1.使用CAST和CONVERT函数时未指定n时,默认长度为30.
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?
Why don't you specify the varchar length? ie:
SELECT CAST('the quick brown fox jumped over the lazy dog' AS VARCHAR(45))
As far as why 30, that's the default length in SQL Server for that type.
From 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.
这篇关于为什么使用 CAST 时 VARCHAR 的默认长度是 30?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么使用 CAST 时 VARCHAR 的默认长度是 30?
基础教程推荐
- oracle区分大小写的原因? 2021-01-01
- 二进制文件到 SQL 数据库 Apache Camel 2021-01-01
- mysql选择动态行值作为列名,另一列作为值 2021-01-01
- 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? 2021-01-01
- 在多列上分布任意行 2021-01-01
- 表 './mysql/proc' 被标记为崩溃,应该修复 2022-01-01
- MySQL 中的类型:BigInt(20) 与 Int(20) 2021-01-01
- 如何根据该 XML 中的值更新 SQL 中的 XML 2021-01-01
- 如何在 SQL 中将 Float 转换为 Varchar 2021-01-01
- 什么是 orradiag_<user>文件夹? 2022-01-01
