Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在sql server中拆分字符串后获取数组长度_Sql_Sql Server_Sql Server 2012 - Fatal编程技术网

在sql server中拆分字符串后获取数组长度

在sql server中拆分字符串后获取数组长度,sql,sql-server,sql-server-2012,Sql,Sql Server,Sql Server 2012,我希望能够在SQL Server中拆分字符串。我有一个示例字符串 1012,1012,1012,1012,1012,1012,1012,1012 这里需要拆分此字符串后的长度 Expected output: 8 如果您只需要分隔字符串中的项数,则根本不需要拆分它-以下是如何操作: 从原始字符串的长度中删除所有分隔符后,将减去字符串的长度。这将提供字符串中分隔符的数量。那么您所要做的就是添加一个,因为您还有一个项目,而不是分隔符: DECLARE @String varchar(50) =

我希望能够在SQL Server中拆分字符串。我有一个示例字符串

1012,1012,1012,1012,1012,1012,1012,1012
这里需要拆分此字符串后的长度

Expected output: 8

如果您只需要分隔字符串中的项数,则根本不需要拆分它-以下是如何操作:
从原始字符串的长度中删除所有分隔符后,将减去字符串的长度。这将提供字符串中分隔符的数量。那么您所要做的就是添加一个,因为您还有一个项目,而不是分隔符:

DECLARE @String varchar(50) = '1012,1012,1012,1012,1012,1012,1012,1012'

SELECT LEN(@String) - LEN(REPLACE(@String, ',', '')) + 1

我认为,为了只找到字符串中的总项,您可能不需要拆分字符串。只需找到
的出现次数

查询

declare @string varchar(100)
set @string = '1012,1012,1012,1012,1012,1012,1012,1012'

select len(@string ) - len(replace(@string ,',', '')) + 1;

你能说出预期的结果吗output@RajaYuvi,预期输出长度:8如果这是输入“10121012101210121012112112112101111011101101101101101101111”。预期输出是什么?可能重复感谢,很好,但无需添加1您有8项和7个分隔符。长度的减法给出了分隔符的数目。@Shohel:你必须加上1@Ullas,“1012”在我的string@Shohel:有8项
LEN(@String)-LEN(REPLACE(@String,,,,'')
将检索逗号总数,即7。所以我们必须加上1。有时候你赢了比赛,有时候你没赢:-)@ZoharPeled:Yep.)