T-SQL象數組一樣處理字符串、分割字符串 在日常的編程過程中,數組是要經常使用到的。在利用SQL對數據庫進行操作時,有時就想在SQL使用數組,比如將1,2,3,4,5拆分成數組。可惜的是在T-SQL中不支持數組。不過還是有變通的辦法。我們可以自己編寫兩函數Get_StrArrayLength(取得字符串的長度-元素的個數)和Get_StrArrayStrOfIndex(按指定符號分割字符串,返回分割后指定索引的第幾個元素)。有了這兩個函數,我們就可以非常方便的在T-SQL中處理形如1,2,3,4,5這樣的字符串了。
T-SQL對字符串的處理能力比較弱,比如我要循環遍歷象1,2,3,4,5這樣的字符串,如果用數組的話,遍歷很簡單,但是T-SQL不支持數組,所以處理下來比較麻煩。下邊的函數,實現了象數組一樣去處理字符串。
一、按指定符號分割字符串,返回分割后的元素個數,方法很簡單,就是看字符串中存在多少個分隔符號,然后再加一,就是要求的結果。
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
(
@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
(
@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
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
轉自哪不記得了,僅個人學習收藏用
