gpt4 book ai didi

sql - 为什么 SQL LEN 函数对于包含多个字符的字符串返回 '1'?

转载 作者:行者123 更新时间:2023-12-04 13:42:43 25 4
gpt4 key购买 nike

简单的问题 - 为什么当我在下面的查询中打印 @len 变量的值时,我得到的值是 1,而不是 12(指定字符串中的字符数)?

DECLARE @string varchar
DECLARE @index int
DECLARE @len int
DECLARE @char char(1)
SET @string = 'content loop'
SET @index = 1
SET @len= LEN(@string)

print @len

最佳答案

您对 @string 的声明是错误的。 varchar 没有长度。

试试这个:

declare @string varchar(255);   -- or whatever

您刚刚了解到这种情况下的默认值为 1。

这在 documentation 中有明确规定.进一步说明,MS 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.

正确的习惯是在使用 varcharnvarchar 时始终包含长度。

关于sql - 为什么 SQL LEN 函数对于包含多个字符的字符串返回 '1'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16803040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com