1 -- 创建函数(作者:csdn邹建) 2 create function getstrarrlength (@str varchar(8000)) 3 returns int 4 as 5 begin 6 declare @int_return int 7 declare @start int 8 declare @next int 9 declare @location int 10 select @str =','+ @str +',' 11 select @str=replace(@str,',,',',') 12 select @start =1 13 select @next =1 14 select @location = charindex(',',@str,@start) 15 while (@location <>0) 16 begin 17 select @start = @location +1 18 select @location = charindex(',',@str,@start) 19 select @next =@next +1 20 end 21 select @int_return = @next-2 22 return @int_return 23 end 24 25 -- 测试示例 26 SELECT [dbo].[getstrarrlength]('1,2,3,4,a,b,c,d') 27 28 --运行结果 29 /* 30 8 31 */ 32 33 /* 34 说明: 35 我开始考虑直接看逗号的个数,用replace替换逗号,求长度差就可以了,但是这里这个函数两个逗号相邻做了处理。 36 */