SQL里類似SPLIT的分割字符串函數(shù)
更新時間:2011年04月20日 01:24:29 作者:
SQL對字符串的處理能力比較弱,比如我要循環(huán)遍歷象1,2,3,4,5這樣的字符串,如果用數(shù)組的話,遍歷很簡單,但是T-SQL不支持?jǐn)?shù)組,所以處理下來比較麻煩。下邊的函數(shù),實現(xiàn)了象數(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 行)
二、按指定符號分割字符串,返回分割后的元素個數(shù),方法很簡單,就是看字符串中存在多少個分隔符號,然后再加一,就是要求的結(jié)果。
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
調(diào)用示例:select dbo.Get_StrArrayLength('78,1,2,3',',')
返回值:4
三、按指定符號分割字符串,返回分割后指定索引的第幾個元素,象數(shù)組一樣方便
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循環(huán)后,@location為0,那默認(rèn)為字符串后邊有一個分隔符號。
return substring(@str,@start,@location-@start)
end
調(diào)用示例:select dbo.Get_StrArrayStrOfIndex('8,9,4',',',2)
返回值:9
三、結(jié)合上邊兩個函數(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
復(fù)制代碼 代碼如下:
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 行)
二、按指定符號分割字符串,返回分割后的元素個數(shù),方法很簡單,就是看字符串中存在多少個分隔符號,然后再加一,就是要求的結(jié)果。
復(fù)制代碼 代碼如下:
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
調(diào)用示例:select dbo.Get_StrArrayLength('78,1,2,3',',')
返回值:4
三、按指定符號分割字符串,返回分割后指定索引的第幾個元素,象數(shù)組一樣方便
復(fù)制代碼 代碼如下:
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循環(huán)后,@location為0,那默認(rèn)為字符串后邊有一個分隔符號。
return substring(@str,@start,@location-@start)
end
調(diào)用示例:select dbo.Get_StrArrayStrOfIndex('8,9,4',',',2)
返回值:9
三、結(jié)合上邊兩個函數(shù),象數(shù)組一樣遍歷字符串中的元素
復(fù)制代碼 代碼如下:
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
相關(guān)文章
SQL server 隨機(jī)數(shù)函數(shù)
在SQL server中,有個隨機(jī)函數(shù)rand(),有不少新手可能不知道存在這個函數(shù),現(xiàn)在我就把這個函數(shù)的一些隨機(jī)數(shù)生成技巧寫出來,這是面向菜鳥的,老鳥請不要拍磚呀,我的頭還不夠硬2009-07-07
Sql注入工具_(dá)動力節(jié)點Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Sql注入工具的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
關(guān)于SQL Server數(shù)據(jù)庫中的用戶權(quán)限和角色管理功能實現(xiàn)
在本文中,我們介紹了在SQL Server中創(chuàng)建用戶、分配權(quán)限和管理角色的基礎(chǔ)知識,請記住定期審查和更新用戶權(quán)限,以符合您組織的安全政策和數(shù)據(jù)訪問要求,這篇文章主要介紹了關(guān)于SQL Server數(shù)據(jù)庫中的用戶權(quán)限和角色管理,需要的朋友可以參考下2024-03-03
SQLServer導(dǎo)出sql文件/表架構(gòu)和數(shù)據(jù)操作步驟
只導(dǎo)出了數(shù)據(jù)庫的sql腳本,而表里的數(shù)據(jù)依然沒有導(dǎo)出來,接下來將為你解決這個問題,感興趣的看下教程哈2013-03-03
Sqlserver中char,nchar,varchar與Nvarchar的區(qū)別分析
Sqlserver中char,nchar,varchar與Nvarchar的區(qū)別分析,使用sqlserver的朋友可以參考下。2011-08-08

