SQLserver字符串分割函数一、按指定符号分割字符串,返回分割后的元素个数,方法很简单,就是看字符串中存在多少个分隔符号,然后再加一,就是要求的结果。
SQLserver字符串分割函数
SQLserver字符串分割函数
CREATE function Get_StrArrayLength
SQLserver字符串分割函数(
SQLserver字符串分割函数  
@str varchar(1024),  --要分割的字符串
SQLserver字符串分割函数
  @split varchar(10)  --分隔符号
SQLserver字符串分割函数
)
SQLserver字符串分割函数
returns int
SQLserver字符串分割函数
as
SQLserver字符串分割函数
begin
SQLserver字符串分割函数  
declare @location int
SQLserver字符串分割函数  
declare @start int
SQLserver字符串分割函数  
declare @length int
SQLserver字符串分割函数
SQLserver字符串分割函数  
set @str=ltrim(rtrim(@str))
SQLserver字符串分割函数  
set @location=charindex(@split,@str)
SQLserver字符串分割函数  
set @length=1
SQLserver字符串分割函数  
while @location<>0
SQLserver字符串分割函数  
begin
SQLserver字符串分割函数    
set @start=@location+1
SQLserver字符串分割函数    
set @location=charindex(@split,@str,@start)
SQLserver字符串分割函数    
set @length=@length+1
SQLserver字符串分割函数  
end
SQLserver字符串分割函数  
return @length
SQLserver字符串分割函数
end
SQLserver字符串分割函数调用示例:
select dbo.Get_StrArrayLength('78,1,2,3',',')
SQLserver字符串分割函数返回值:
4
SQLserver字符串分割函数
SQLserver字符串分割函数二、按指定符号分割字符串,返回分割后指定索引的第几个元素,象数组一样方便
SQLserver字符串分割函数
SQLserver字符串分割函数
CREATE function Get_StrArrayStrOfIndex
SQLserver字符串分割函数(
SQLserver字符串分割函数  
@str varchar(1024),  --要分割的字符串
SQLserver字符串分割函数
  @split varchar(10),  --分隔符号
SQLserver字符串分割函数
  @index int --取第几个元素
SQLserver字符串分割函数
)
SQLserver字符串分割函数
returns varchar(1024)
SQLserver字符串分割函数
as
SQLserver字符串分割函数
begin
SQLserver字符串分割函数  
declare @location int
SQLserver字符串分割函数  
declare @start int
SQLserver字符串分割函数  
declare @next int
SQLserver字符串分割函数  
declare @seed int
SQLserver字符串分割函数
SQLserver字符串分割函数  
set @str=ltrim(rtrim(@str))
SQLserver字符串分割函数  
set @start=1
SQLserver字符串分割函数  
set @next=1
SQLserver字符串分割函数  
set @seed=len(@split)
SQLserver字符串分割函数  
SQLserver字符串分割函数  
set @location=charindex(@split,@str)
SQLserver字符串分割函数  
while @location<>0 and @index>@next
SQLserver字符串分割函数  
begin
SQLserver字符串分割函数    
set @start=@location+@seed
SQLserver字符串分割函数    
set @location=charindex(@split,@str,@start)
SQLserver字符串分割函数    
set @next=@next+1
SQLserver字符串分割函数  
end
SQLserver字符串分割函数  
if @location =0 select @location =len(@str)+1
SQLserver字符串分割函数 
--这儿存在两种情况:1、字符串不存在分隔符号 2、字符串中存在分隔符号,跳出while循环后,@location为0,那默认为字符串后边有一个分隔符号。
SQLserver字符串分割函数
  
SQLserver字符串分割函数  
return substring(@str,@start,@location-@start)
SQLserver字符串分割函数
end
SQLserver字符串分割函数调用示例:
select dbo.Get_StrArrayStrOfIndex('8,9,4',',',2)
SQLserver字符串分割函数返回值:
9
SQLserver字符串分割函数
SQLserver字符串分割函数三、结合上边两个函数,象数组一样遍历字符串中的元素
SQLserver字符串分割函数
SQLserver字符串分割函数
create   function   f_splitstr(@SourceSql   varchar(8000),@StrSeprate   varchar(100))   
SQLserver字符串分割函数  
returns   @temp   table(F1   varchar(100))   
SQLserver字符串分割函数  
as     
SQLserver字符串分割函数  
begin   
SQLserver字符串分割函数  
declare   @ch   as   varchar(100)   
SQLserver字符串分割函数  
set   @SourceSql=@SourceSql+@StrSeprate     
SQLserver字符串分割函数  
while(@SourceSql<>'')   
SQLserver字符串分割函数                  
begin   
SQLserver字符串分割函数                  
set   @ch=left(@SourceSql,charindex(',',@SourceSql,1)-1)   
SQLserver字符串分割函数  
insert   @temp   values(@ch)   
SQLserver字符串分割函数  
set   @SourceSql=stuff(@SourceSql,1,charindex(',',@SourceSql,1),'')   
SQLserver字符串分割函数                  
end   
SQLserver字符串分割函数  
return   
SQLserver字符串分割函数  
end   
SQLserver字符串分割函数    
SQLserver字符串分割函数
SQLserver字符串分割函数
----调用
SQLserver字符串分割函数
  select   *   from   dbo.f_splitstr('1,2,3,4',','
SQLserver字符串分割函数
--结果:
SQLserver字符串分割函数
1
SQLserver字符串分割函数
2
SQLserver字符串分割函数
3
SQLserver字符串分割函数
4
SQLserver字符串分割函数
SQLserver字符串分割函数
SQLserver字符串分割函数
SQLserver字符串分割函数
SQLserver字符串分割函数
SQLserver字符串分割函数Trackback: http:
//tb.blog.csdn.net/TrackBack.aspx?PostId=1639974
SQLserver字符串分割函数

相关文章: