亚洲韩日午夜视频,欧美日韩在线精品一区二区三区,韩国超清无码一区二区三区,亚洲国产成人影院播放,久草新在线,在线看片AV色

您好,歡迎來到思海網絡,我們將竭誠為您提供優質的服務! 誠征網絡推廣 | 網站備案 | 幫助中心 | 軟件下載 | 購買流程 | 付款方式 | 聯系我們 [ 會員登錄/注冊 ]
促銷推廣
客服中心
業務咨詢
有事點擊這里…  531199185
有事點擊這里…  61352289
點擊這里給我發消息  81721488
有事點擊這里…  376585780
有事點擊這里…  872642803
有事點擊這里…  459248018
有事點擊這里…  61352288
有事點擊這里…  380791050
技術支持
有事點擊這里…  714236853
有事點擊這里…  719304487
有事點擊這里…  1208894568
有事點擊這里…  61352289
在線客服
有事點擊這里…  531199185
有事點擊這里…  61352288
有事點擊這里…  983054746
有事點擊這里…  893984210
當前位置:首頁 >> 技術文章 >> 文章瀏覽
技術文章

MS SQL Server里類似SPLIT的分割字符串函數

添加時間:2013-3-16 15:18:36  添加: 思海網絡 
T-SQL對字符串的處理能力比較弱,比如我要循環遍歷象1,2,3,4,5這樣的字符串,如果用數組的話,遍歷很簡單,但是T-SQL不支持數組,所以處理下來比較麻煩。下邊的函數,實現了象數組一樣去處理字符串。
一,用臨時表作為數組
create   function   f_split(@c   varchar(2000),@split   varchar(2))  
  returns   @t   table(col   varchar(20))  
  as  
    begin  
   
      while(charindex(@split,@c)<>0)  
        begin  
          insert   @t(col)   values   (substring(@c,1,charindex(@split,@c)-1))  
          set   @c   =   stuff(@c,1,charindex(@split,@c),'')  
        end  
      insert   @t(col)   values   (@c)  
      return  
    end  
  go  
   
  select   *   from   dbo.f_split('dfkd,dfdkdf,dfdkf,dffjk',',')  
   
  drop   function   f_split  

  col                                      
  --------------------    
  dfkd  
  dfdkdf  
  dfdkf  
  dffjk  
   
  (所影響的行數為   4   行)


二、按指定符號分割字符串,返回分割后的元素個數,方法很簡單,就是看字符串中存在多少個分隔符號,然后再加一,就是要求的結果。
CREATE function Get_StrArrayLength
(
 @str varchar(1024),  --要分割的字符串
 @split varchar(10)  --分隔符號
)
returns int
as
begin
 declare @location int
 declare @start int
 declare @length int

 set @str=ltrim(rtrim(@str))
 set @location=charindex(@split,@str)
 set @length=1
 while @location<>0
 begin
   set @start=@location+1
   set @location=charindex(@split,@str,@start)
   set @length=@length+1
 end
 return @length
end
調用示例:select dbo.Get_StrArrayLength('78,1,2,3',',')
返回值:4

三、按指定符號分割字符串,返回分割后指定索引的第幾個元素,象數組一樣方便
CREATE function Get_StrArrayStrOfIndex
(
 @str varchar(1024),  --要分割的字符串
 @split varchar(10),  --分隔符號
 @index int --取第幾個元素
)
returns varchar(1024)
as
begin
 declare @location int
 declare @start int
 declare @next int
 declare @seed int

 set @str=ltrim(rtrim(@str))
 set @start=1
 set @next=1
 set @seed=len(@split)
 
 set @location=charindex(@split,@str)
 while @location<>0 and @index>@next
 begin
   set @start=@location+@seed
   set @location=charindex(@split,@str,@start)
   set @next=@next+1
 end
 if @location =0 select @location =len(@str)+1
--這兒存在兩種情況:1、字符串不存在分隔符號 2、字符串中存在分隔符號,跳出while循環后,@location為0,那默認為字符串后邊有一個分隔符號。
 
 return substring(@str,@start,@location-@start)
end
調用示例:select dbo.Get_StrArrayStrOfIndex('8,9,4',',',2)
返回值:9

三、結合上邊兩個函數,象數組一樣遍歷字符串中的元素

declare @str varchar(50)
set @str='1,2,3,4,5'
declare @next int  
set @next=1
while @next<=dbo.Get_StrArrayLength(@str,',')
begin
 print dbo.Get_StrArrayStrOfIndex(@str,',',@next)
 set @next=@next+1
end

調用結果:
1
2
3
4
5
關鍵字:字符串、數組、函數、SQL Server
分享到:

頂部 】 【 關閉
版權所有:佛山思海電腦網絡有限公司 ©1998-2024 All Rights Reserved.
聯系電話:(0757)22630313、22633833
中華人民共和國增值電信業務經營許可證: 粵B1.B2-20030321 備案號:粵B2-20030321-1
網站公安備案編號:44060602000007 交互式欄目專項備案編號:200303DD003  
察察 工商 網安 舉報有獎  警警  手機打開網站