SQL中实现SPLIT函数几种方法总结(必看篇)
程序员文章站
2023-12-21 09:41:04
例1
代码如下
create function f_split(@sourcesql varchar(8000),@strseprate varchar(10)...
例1
代码如下
create function f_split(@sourcesql varchar(8000),@strseprate varchar(10)) returns @temp table(a varchar(100)) --实现split功能 的函数 --date :2003-10-14 as begin declare @i int set @sourcesql=rtrim(ltrim(@sourcesql)) set @i=charindex(@strseprate,@sourcesql) while @i>=1 begin insert @temp values(left(@sourcesql,@i-1)) set @sourcesql=substring(@sourcesql,@i+1,len(@sourcesql)-@i) set @i=charindex(@strseprate,@sourcesql) end if @sourcesql<>'' insert @temp values(@sourcesql) return end select * from dbo.f_split('1,2,3,4',',') a -------------------- 1 2 3 4 (所影响的行数为 4 行)
例2
代码如下
--sql server split函数 --author:zc_0101 --说明: --支持分割符多字节 --使用方法 --select * from dbo.f_sqlserver_split('1203401230105045','0') --select * from dbo.f_sqlserver_split('abc1234a12348991234','1234') --select * from dbo.f_sqlserver_split('abc',',') create function f_sqlserver_split(@long_str varchar(8000),@split_str varchar(100)) returns @tmp table( id int identity primary key, short_str varchar(8000) ) as begin declare @long_str_tmp varchar(8000),@short_str varchar(8000),@split_str_length int set @split_str_length = len(@split_str) if charindex(@split_str,@long_str)=1 set @long_str_tmp=substring(@long_str,@split_str_length+1,len(@long_str)-@split_str_length) else set @long_str_tmp=@long_str if charindex(reverse(@split_str),reverse(@long_str_tmp))>1 set @long_str_tmp=@long_str_tmp+@split_str else set @long_str_tmp=@long_str_tmp if charindex(@split_str,@long_str_tmp)=0 insert into @tmp select @long_str_tmp else begin while charindex(@spl(www.jb51.net)it_str,@long_str_tmp)>0 begin set @short_str=substring(@long_str_tmp,1,charindex(@split_str,@long_str_tmp)-1) declare @long_str_tmp_len int,@split_str_position_end int set @long_str_tmp_len = len(@long_str_tmp) set @split_str_position_end = len(@short_str)+@split_str_length set @long_str_tmp=reverse(substring(reverse(@long_str_tmp),1,@long_str_tmp_len-@split_str_position_end)) if @short_str<>'' insert into @tmp select @short_str end end return end
例3
sql2000andsql2005实用的split函数
代码如下
sql2000 create function [dbo].[splitstring_array] ( @string nvarchar(4000),@split char(1) ) returns @array table ( onestr nvarchar(100) ) as begin declare @v_code varchar(60) --zell 2006-05-26 --set @string = replace(@string,' ',@split) --set @string = replace(@string,',',@split) while len(@string) > 0 begin if charindex(@split,@string,1) != 0 begin set @v_code = substring(@string,1,charindex(@split,@string,1)-1) set @string = substring(@string,charindex(@split,@string,1)+1,len(@string)) end else if charindex(@split,@string,1) = 0 begin set @v_code = @string set @string = '' end insert into @array(onestr) values(@v_code) end return end sql2005 create function [dbo].[func_splitid] (@str varchar(max),@split varchar(10)) returns @t table (c1 nvarchar(100)) as begin declare @x xml set @x = convert(xml,'<items><item id="' + replace(@str, @split, '"/><item id="') + '"/></items>') insert into @t select x.item.value('@id[1]', 'nvarchar(100)') from @x.nodes('//items/item') as x(item) return end
以上这篇sql中实现split函数几种方法总结(必看篇)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。