varChar 和 Char 在强制转换时表现不同

发布于 2024-09-26 07:05:23 字数 151 浏览 0 评论 0原文

为什么此查询的输出不同

SELECT DATALENGTH(CAST('test' AS VARCHAR)), DATALENGTH(CAST('test' AS CHAR))

outPut:

4,30

Why the output is different of this query

SELECT DATALENGTH(CAST('test' AS VARCHAR)), DATALENGTH(CAST('test' AS CHAR))

outPut:

4,30

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绅刃 2024-10-03 07:05:23

VarChar 始终根据传递的字符串长度进行调整。因此输出为 4。CHAR

的默认长度为 30,因此输出为 30。

VarChar always adjusts to the length of the string passed. hence the output is 4.

CHAR has default length of 30 and hence the output is 30.

同展鸳鸯锦 2024-10-03 07:05:23

因为"使用CAST和CONVERT函数时未指定n时,默认长度为30”。但是,当 DataLength 应用于 varchar 字段时,它会忽略尾随空格,而对于 char 字段,长度只是字段本身的大小。

Because "When n is not specified when using the CAST and CONVERT functions, the default length is 30". But when DataLength is applied to a varchar field, it ignores trailing spaces, while for a char field the length is just the size of the field itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文