SQL里类似SPLIT的分割字符串函数T-SQL对字符串的处理能力比较弱,比如我要循环遍历象1,2,3,4,5这样的字符串,如果用数组的话,遍历很简单,但是T-SQL不支持数组,所以处理下来比较麻烦。下边的函数,实现了象数组一样去处理字符串。
SQL里类似SPLIT的分割字符串函数一,用临时表作为数组
SQL里类似SPLIT的分割字符串函数
create   function   f_split(@c   varchar(2000),@split   varchar(2))   
SQL里类似SPLIT的分割字符串函数  
returns   @t   table(col   varchar(20))   
SQL里类似SPLIT的分割字符串函数  
as   
SQL里类似SPLIT的分割字符串函数    
begin   
SQL里类似SPLIT的分割字符串函数    
SQL里类似SPLIT的分割字符串函数      
while(charindex(@split,@c)<>0)   
SQL里类似SPLIT的分割字符串函数        
begin   
SQL里类似SPLIT的分割字符串函数          
insert   @t(col)   values   (substring(@c,1,charindex(@split,@c)-1))   
SQL里类似SPLIT的分割字符串函数          
set   @c   =   stuff(@c,1,charindex(@split,@c),'')   
SQL里类似SPLIT的分割字符串函数        
end   
SQL里类似SPLIT的分割字符串函数      
insert   @t(col)   values   (@c)   
SQL里类似SPLIT的分割字符串函数      
return   
SQL里类似SPLIT的分割字符串函数    
end   
SQL里类似SPLIT的分割字符串函数  
go   
SQL里类似SPLIT的分割字符串函数    
SQL里类似SPLIT的分割字符串函数  
select   *   from   dbo.f_split('dfkd,dfdkdf,dfdkf,dffjk',',')   
SQL里类似SPLIT的分割字符串函数    
SQL里类似SPLIT的分割字符串函数  
drop   function   f_split  
SQL里类似SPLIT的分割字符串函数  col                                       
SQL里类似SPLIT的分割字符串函数  
--------------------     
SQL里类似SPLIT的分割字符串函数
  dfkd   
SQL里类似SPLIT的分割字符串函数  dfdkdf   
SQL里类似SPLIT的分割字符串函数  dfdkf   
SQL里类似SPLIT的分割字符串函数  dffjk   
SQL里类似SPLIT的分割字符串函数    
SQL里类似SPLIT的分割字符串函数  (所影响的行数为   
4   行)
SQL里类似SPLIT的分割字符串函数
SQL里类似SPLIT的分割字符串函数
SQL里类似SPLIT的分割字符串函数二、按指定符号分割字符串,返回分割后的元素个数,方法很简单,就是看字符串中存在多少个分隔符号,然后再加一,就是要求的结果。
SQL里类似SPLIT的分割字符串函数
CREATE function Get_StrArrayLength
SQL里类似SPLIT的分割字符串函数(
SQL里类似SPLIT的分割字符串函数 
@str varchar(1024),  --要分割的字符串
SQL里类似SPLIT的分割字符串函数
 @split varchar(10)  --分隔符号
SQL里类似SPLIT的分割字符串函数
)
SQL里类似SPLIT的分割字符串函数
returns int
SQL里类似SPLIT的分割字符串函数
as
SQL里类似SPLIT的分割字符串函数
begin
SQL里类似SPLIT的分割字符串函数 
declare @location int
SQL里类似SPLIT的分割字符串函数 
declare @start int
SQL里类似SPLIT的分割字符串函数 
declare @length int
SQL里类似SPLIT的分割字符串函数
SQL里类似SPLIT的分割字符串函数 
set @str=ltrim(rtrim(@str))
SQL里类似SPLIT的分割字符串函数 
set @location=charindex(@split,@str)
SQL里类似SPLIT的分割字符串函数 
set @length=1
SQL里类似SPLIT的分割字符串函数 
while @location<>0
SQL里类似SPLIT的分割字符串函数 
begin
SQL里类似SPLIT的分割字符串函数   
set @start=@location+1
SQL里类似SPLIT的分割字符串函数   
set @location=charindex(@split,@str,@start)
SQL里类似SPLIT的分割字符串函数   
set @length=@length+1
SQL里类似SPLIT的分割字符串函数 
end
SQL里类似SPLIT的分割字符串函数 
return @length
SQL里类似SPLIT的分割字符串函数
end
SQL里类似SPLIT的分割字符串函数调用示例:
select dbo.Get_StrArrayLength('78,1,2,3',',')
SQL里类似SPLIT的分割字符串函数返回值:
4
SQL里类似SPLIT的分割字符串函数
SQL里类似SPLIT的分割字符串函数三、按指定符号分割字符串,返回分割后指定索引的第几个元素,象数组一样方便
SQL里类似SPLIT的分割字符串函数
CREATE function Get_StrArrayStrOfIndex
SQL里类似SPLIT的分割字符串函数(
SQL里类似SPLIT的分割字符串函数 
@str varchar(1024),  --要分割的字符串
SQL里类似SPLIT的分割字符串函数
 @split varchar(10),  --分隔符号
SQL里类似SPLIT的分割字符串函数
 @index int --取第几个元素
SQL里类似SPLIT的分割字符串函数
)
SQL里类似SPLIT的分割字符串函数
returns varchar(1024)
SQL里类似SPLIT的分割字符串函数
as
SQL里类似SPLIT的分割字符串函数
begin
SQL里类似SPLIT的分割字符串函数 
declare @location int
SQL里类似SPLIT的分割字符串函数 
declare @start int
SQL里类似SPLIT的分割字符串函数 
declare @next int
SQL里类似SPLIT的分割字符串函数 
declare @seed int
SQL里类似SPLIT的分割字符串函数
SQL里类似SPLIT的分割字符串函数 
set @str=ltrim(rtrim(@str))
SQL里类似SPLIT的分割字符串函数 
set @start=1
SQL里类似SPLIT的分割字符串函数 
set @next=1
SQL里类似SPLIT的分割字符串函数 
set @seed=len(@split)
SQL里类似SPLIT的分割字符串函数 
SQL里类似SPLIT的分割字符串函数 
set @location=charindex(@split,@str)
SQL里类似SPLIT的分割字符串函数 
while @location<>0 and @index>@next
SQL里类似SPLIT的分割字符串函数 
begin
SQL里类似SPLIT的分割字符串函数   
set @start=@location+@seed
SQL里类似SPLIT的分割字符串函数   
set @location=charindex(@split,@str,@start)
SQL里类似SPLIT的分割字符串函数   
set @next=@next+1
SQL里类似SPLIT的分割字符串函数 
end
SQL里类似SPLIT的分割字符串函数 
if @location =0 select @location =len(@str)+1 
SQL里类似SPLIT的分割字符串函数
--这儿存在两种情况:1、字符串不存在分隔符号 2、字符串中存在分隔符号,跳出while循环后,@location为0,那默认为字符串后边有一个分隔符号。
SQL里类似SPLIT的分割字符串函数
 
SQL里类似SPLIT的分割字符串函数 
return substring(@str,@start,@location-@start)
SQL里类似SPLIT的分割字符串函数
end
SQL里类似SPLIT的分割字符串函数调用示例:
select dbo.Get_StrArrayStrOfIndex('8,9,4',',',2)
SQL里类似SPLIT的分割字符串函数返回值:
9
SQL里类似SPLIT的分割字符串函数
SQL里类似SPLIT的分割字符串函数三、结合上边两个函数,象数组一样遍历字符串中的元素
SQL里类似SPLIT的分割字符串函数
SQL里类似SPLIT的分割字符串函数
declare @str varchar(50)
SQL里类似SPLIT的分割字符串函数
set @str='1,2,3,4,5'
SQL里类似SPLIT的分割字符串函数
declare @next int  
SQL里类似SPLIT的分割字符串函数
set @next=1
SQL里类似SPLIT的分割字符串函数
while @next<=dbo.Get_StrArrayLength(@str,',')
SQL里类似SPLIT的分割字符串函数
begin
SQL里类似SPLIT的分割字符串函数 
print dbo.Get_StrArrayStrOfIndex(@str,',',@next)
SQL里类似SPLIT的分割字符串函数 
set @next=@next+1
SQL里类似SPLIT的分割字符串函数
end
SQL里类似SPLIT的分割字符串函数
SQL里类似SPLIT的分割字符串函数调用结果:
SQL里类似SPLIT的分割字符串函数
1
SQL里类似SPLIT的分割字符串函数
2
SQL里类似SPLIT的分割字符串函数
3
SQL里类似SPLIT的分割字符串函数
4
SQL里类似SPLIT的分割字符串函数
5

相关文章:

  • 2021-08-16
  • 2022-12-23
  • 2021-06-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-22
  • 2022-02-02
  • 2022-12-23
相关资源
相似解决方案