分割字符串
T-SQL對(duì)字符串的處理能力比較弱,比如我要循環(huán)遍歷象1,2,3,4,5這樣的字符串,如果用數(shù)組的話,遍歷很簡(jiǎn)單,但是T-SQL不支持?jǐn)?shù)組,所以處理下來(lái)比較麻煩。下邊的函數(shù),實(shí)現(xiàn)了象數(shù)組一樣去處理字符串。
一,用臨時(shí)表作為數(shù)組
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??
???
(所影響的行數(shù)為?? 4?? 行)
二、按指定符號(hào)分割字符串,返回分割后的元素個(gè)數(shù),方法很簡(jiǎn)單,就是看字符串中存在多少個(gè)分隔符號(hào),然后再加一,就是要求的結(jié)果。
CREATE function Get_StrArrayLength
(
@str varchar(1024), --要分割的字符串
@split varchar(10) --分隔符號(hào)
)
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
調(diào)用示例:select dbo.Get_StrArrayLength('78,1,2,3',',')
返回值:4
三、按指定符號(hào)分割字符串,返回分割后指定索引的第幾個(gè)元素,象數(shù)組一樣方便
CREATE function Get_StrArrayStrOfIndex
(
@str varchar(1024), --要分割的字符串
@split varchar(10), --分隔符號(hào)
@index int --取第幾個(gè)元素
)
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、字符串不存在分隔符號(hào) 2、字符串中存在分隔符號(hào),跳出while循環(huán)后,@location為0,那默認(rèn)為字符串后邊有一個(gè)分隔符號(hào)。
return substring(@str,@start,@location-@start)
end
調(diào)用示例:select dbo.Get_StrArrayStrOfIndex('8,9,4',',',2)
返回值:9
三、結(jié)合上邊兩個(gè)函數(shù),象數(shù)組一樣遍歷字符串中的元素
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
調(diào)用結(jié)果:
1
2
3
4
5
----------------------------------------分割實(shí)例-----------------------------------------------------------------------
創(chuàng)建函數(shù):
CREATE?? function?? [dbo].[SplitStr]
(
??? @SourceSql?? varchar(8000),
??? @StrSeprate?? varchar(100))
??? returns?? @temp?? table(F1?? varchar(100)
)??
? as????
? begin??
? declare?? @ch?? as?? varchar(100)??
? set?? @SourceSql=@SourceSql+@StrSeprate????
? while(@SourceSql<>'')??
????????????????? begin??
????????????????? set?? @ch=left(@SourceSql,charindex(',',@SourceSql,1)-1)??
? insert?? @temp?? values(@ch)??
? set?? @SourceSql=stuff(@SourceSql,1,charindex(',',@SourceSql,1),'')??
????????????????? end??
? return??
? end
應(yīng)用(存儲(chǔ)過(guò)程):
CREATE PROCEDURE [dbo].[UP_Student_GetListByStatus]
??? @StatusList varchar(max)
AS
SELECT [Student].*
FROM Student
WHERE
??? [Student].[PK_Status] IN (SELECT F1 FROM dbo.SplitStr(@StatusList,','))
轉(zhuǎn)載于:https://www.cnblogs.com/Sue_/articles/1705486.html
總結(jié)
- 上一篇: SetNoneScaleMutableG
- 下一篇: .Net Discovery系列之十一-