因为业务需要,自己动手写了一个T-Sql实现的一个Split函数,本来想在网上看看有没有,一看看都不是很适用。现在拿现来分享一下(缺点:分隔符号只考虑一个字节的,有其它需要自己修改了B-\):
create function Split(@str nvarchar(max),@sp nvarchar(1))returns @table table( id int)asbeginset @str = rtrim(ltrim(@str))if len(@str)<1returndeclare @location int,@temstr varchar(10),@curr varchar(2)set @location = 1set @temstr = ''while @location <(len(@str)+1)beginset @curr = substring(@str,@location,1)if @curr = @spbegininsert into @table values(@temstr)set @temstr = ''endelsebeginset @temstr=@temstr+@currif @location = len(@str)insert into @table values(@temstr)endset @location = @location+1endreturnendgo调用方法:
select * from dbo.Split('1,23,34,4,5',',')简单吧?合用的话留个言哦,谢!
已经修改:字符串的长度,和格式,但是,没有作charindex的处理,因为当前函数不打算作对字符串内容的切割,只切割类似整数数据的字符串,详细看返回类型,只返回整数列表。
转载于:https://www.cnblogs.com/alexwem/archive/2011/10/26/split.html
相关资源:sqlserver 实现 行转列 split 分割的函数