sql分割字符串函数

时间:2024-10-13 05:00:41

1、char坡纠课柩index内置函数返回指定字符所出现的位置。第一个参数为目标字符串,即查找的字符串;第二个参数为被查找的字符串;第三个参数为开始查找位置,为空时默认从第一位查找

sql分割字符串函数

2、创建函数。

sql分割字符串函数
sql分割字符串函数
sql分割字符串函数

3、测试函数。

sql分割字符串函数

4、函数脚本;create function [dbo].[fn_Split](@str varchar(max), --原字符串刺胳挤萧@fgzf varchar(10) --分割符号)returns @table table --返回(id int identity(1,1),value varchar(max))asbegindeclare @len int = 1;declare @len1 int = 1;declare @count int = 0;set @count = len(@str);if(@count > 0)begin while @len <> 0 begin set @len1 = @len;if(@len = 1) begin set @len = CHARINDEX(@fgzf,@str,@len);end else begin set @len = CHARINDEX(@fgzf,@str,@len + 1);end if(@len1 = 1)begin if(@len <> 0)begin insert into @table(value)select SUBSTRING(@str,@len1,@len-1);endelse begin insert into @table(value)select @str;end endelse if (@len <> 0) begin insert into @table(value)select SUBSTRING(@str,@len1 + 1,@len - @len1 -1);end if(@len = 0 and @len1 > 1) begin insert into @table(value)select SUBSTRING(@str,@len1 + 1,len(@str) - @len1);end end endreturn;end

© 手抄报圈