T-SQL对字符串的处理能力比较弱,比如我要循环遍历象1,2,3,4,5这样的字符串,如果用数组的话,遍历很简单,但是T-SQL不支持数组,所以处理下来比较麻烦。下边的函数,实现了象数组一样去处理字符串。
一,用临时表作为数组。

SQL分割字符串详解create   function   f_split(@c   varchar(2000),@split   varchar(2))   
SQL分割字符串详解
returns   @t   table(col   varchar(20))   
SQL分割字符串详解
as   
SQL分割字符串详解    
begin   
SQL分割字符串详解    
SQL分割字符串详解      
while(charindex(@split,@c)<>0)   
SQL分割字符串详解        
begin   
SQL分割字符串详解          
insert   @t(col)   values   (substring(@c,1,charindex(@split,@c)-1))   
SQL分割字符串详解          
set   @c   =   stuff(@c,1,charindex(@split,@c),'')   
SQL分割字符串详解        
end   
SQL分割字符串详解      
insert   @t(col)   values   (@c)   
SQL分割字符串详解      
return   
SQL分割字符串详解    
end   
SQL分割字符串详解
go   
SQL分割字符串详解    
SQL分割字符串详解
select   *   from   dbo.f_split('dfkd,dfdkdf,dfdkf,dffjk',',')   
SQL分割字符串详解    
SQL分割字符串详解
drop   function   f_split 
SQL分割字符串详解col                                       
SQL分割字符串详解
--------------------     
SQL分割字符串详解
dfkd   
SQL分割字符串详解dfdkdf   
SQL分割字符串详解dfdkf   
SQL分割字符串详解dffjk   
SQL分割字符串详解


   
(所影响的行数为   4   行)
 
二、按指定符号分割字符串,返回分割后的元素个数,方法很简单,就是看字符串中存在多少个分隔符号,然后再加一,就是要求的结果。

SQL分割字符串详解CREATE function Get_StrArrayLength
SQL分割字符串详解(
SQL分割字符串详解
@str varchar(1024), --要分割的字符串
SQL分割字符串详解
@split varchar(10--分隔符号
SQL分割字符串详解
)
SQL分割字符串详解
returns int
SQL分割字符串详解
as
SQL分割字符串详解
begin
SQL分割字符串详解
declare @location int
SQL分割字符串详解
declare @start int
SQL分割字符串详解
declare @length int
SQL分割字符串详解 
SQL分割字符串详解
set @str=ltrim(rtrim(@str))
SQL分割字符串详解
set @location=charindex(@split,@str)
SQL分割字符串详解
set @length=1
SQL分割字符串详解
while @location<>0
SQL分割字符串详解
begin
SQL分割字符串详解   
set @start=@location+1
SQL分割字符串详解   
set @location=charindex(@split,@str,@start)
SQL分割字符串详解   
set @length=@length+1
SQL分割字符串详解
end
SQL分割字符串详解
return @length
SQL分割字符串详解
end
SQL分割字符串详解


调用示例:

SQL分割字符串详解select dbo.Get_StrArrayLength('78,1,2,3',',')


返回值:4
 
三、按指定符号分割字符串,返回分割后指定索引的第几个元素,象数组一样方便

SQL分割字符串详解CREATE function Get_StrArrayStrOfIndex
SQL分割字符串详解(
SQL分割字符串详解
@str varchar(1024), --要分割的字符串
SQL分割字符串详解
@split varchar(10), --分隔符号
SQL分割字符串详解
@index int --取第几个元素
SQL分割字符串详解
)
SQL分割字符串详解
returns varchar(1024)
SQL分割字符串详解
as
SQL分割字符串详解
begin
SQL分割字符串详解
declare @location int
SQL分割字符串详解
declare @start int
SQL分割字符串详解
declare @next int
SQL分割字符串详解
declare @seed int
SQL分割字符串详解 
SQL分割字符串详解
set @str=ltrim(rtrim(@str))
SQL分割字符串详解
set @start=1
SQL分割字符串详解
set @next=1
SQL分割字符串详解
set @seed=len(@split)
SQL分割字符串详解 
SQL分割字符串详解
set @location=charindex(@split,@str)
SQL分割字符串详解
while @location<>0 and @index>@next
SQL分割字符串详解
begin
SQL分割字符串详解   
set @start=@location+@seed
SQL分割字符串详解   
set @location=charindex(@split,@str,@start)
SQL分割字符串详解   
set @next=@next+1
SQL分割字符串详解
end
SQL分割字符串详解
if @location =0 select @location =len(@str)+1 
SQL分割字符串详解
--这儿存在两种情况:1、字符串不存在分隔符号 2、字符串中存在分隔符号,跳出while循环后,@location为0,那默认为字符串后边有一个分隔符号。
SQL分割字符串详解
 
SQL分割字符串详解
return substring(@str,@start,@location-@start)
SQL分割字符串详解
end
SQL分割字符串详解


调用示例:

SQL分割字符串详解select dbo.Get_StrArrayStrOfIndex('8,9,4',',',2)


返回值:9
 
三、结合上边两个函数,象数组一样遍历字符串中的元素
 

SQL分割字符串详解declare @str varchar(50)
SQL分割字符串详解
set @str='1,2,3,4,5'
SQL分割字符串详解
declare @next int 
SQL分割字符串详解
set @next=1
SQL分割字符串详解
while @next<=dbo.Get_StrArrayLength(@str,',')
SQL分割字符串详解
begin
SQL分割字符串详解
print dbo.Get_StrArrayStrOfIndex(@str,',',@next)
SQL分割字符串详解
set @next=@next+1
SQL分割字符串详解
end


 
调用结果:

SQL分割字符串详解1
SQL分割字符串详解
2
SQL分割字符串详解
3
SQL分割字符串详解
4
SQL分割字符串详解
5 
SQL分割字符串详解
//-------------------------------------------------------------------------------------------------------------------------
SQL分割字符串详解
select f1,f2,f3,left(A,charindex('_',A)-1)
SQL分割字符串详解
from 表名 
SQL分割字符串详解
order by left(A,charindex('_',A)-1)

相关文章:

  • 2022-02-03
  • 2021-11-30
  • 2022-12-23
  • 2021-08-09
猜你喜欢
  • 2022-01-27
  • 2021-08-23
  • 2021-12-18
  • 2021-06-28
  • 2021-10-11
  • 2022-02-25
相关资源
相似解决方案