例1
**如下
create function f_split(@sourcesql varchar(8000),@strseprate varchar(10))
returns @temp table(a varchar(100))
--實現sp功能 的函式
--date :2003-10-14
as begin
declare @i int
set @sourcesql=rtrim(ltrim(@sourcesql))
set @i=charindex(@strseprate,@sourcesql)
while @i>=1
begin
insert @temp values(left(@sourcesql,@i-1))
set @sourcesql=substring(@sourcesql,@i+1,len(@sourcesql)-@i)
set @i=charindex(@strseprate,@sourcesql)
endif @sourcesql<>''
insert @temp values(@sourcesql)
return
endselect * from dbo.f_split('1,2,3,4',',')
a -------------------- 12
34(所影響的行數為 4 行)
例2**如下
--sql server split函式
--author:zc_0101
--說明:
--支援分割符多位元組
--使用方法
--select * from dbo.f_sqlserver_split('1203401230105045','0')
--select * from dbo.f_sqlserver_split('abc1234a12348991234','1234')
--select * from dbo.f_sqlserver_split('abc',',')
create function f_sqlserver_split(@long_str varchar(8000),@split_str varchar(100))
returns @tmp table(
id int identity primary key,
short_str varchar(8000)
) as
begin
declare @long_str_tmp varchar(8000),@short_str varchar(8000),@split_str_length int
set @split_str_length = len(@split_str)
if charindex(@split_str,@long_str)=1
set @long_str_tmp=substring(@long_str,@split_str_length+1,len(@long_str)-@split_str_length)
else
set @long_str_tmp=@long_str
if charindex(reverse(@split_str),reverse(@long_str_tmp))>1
set @long_str_tmp=@long_str_tmp+@split_str
else
set @long_str_tmp=@long_str_tmp
if charindex(@split_str,@long_str_tmp)=0
insert into @tmp select @long_str_tmp
else
begin
while charindex(@spl(www.jb51.net)it_str,@long_str_tmp)>0
begin
set @short_str=substring(@long_str_tmp,1,charindex(@split_str,@long_str_tmp)-1)
declare @long_str_tmp_len int,@split_str_position_end int
set @long_str_tmp_len = len(@long_str_tmp)
set 程式設計客棧@split_str_position_end = len(@short_str)+@split_str_length
set @long_str_tmp=reverse(substring(reverse(@long_str_tmp),1,@long_str_tmp_len-@split_str_position_end))
if @short_str<>'' insert into @tmp select @short_str
end
endreturn
end例3
sql2000andsql2005實用的split函式
**如下
sql2000
create functioqdznmn [dbo].[splitstring_array]
( @string n程式設計客棧varchar(4000),@split char(1)
) returns @array table
( onestr nvarchar(100)
) as
begin
declare @v_code varchar(60)
--zell 2006-05-26
--set @string = replace(@string,' ',@split)
--set @string = replace(@string,',',@split)
while len(@string) > 0
begin
if charindex(@split,@string,1) != 0
begin
set @v_code = substring(@string,1,charindex(@split,@string,1)-1)
set @string = substring(@string,charindex(@split,@string,1)+1,len(@string))
endelse if charindex(@split,@string,1) = 0
begin
set @v_code = @string
set @string = ''
endinsert into @array(onestr) values(@v_code)
endreturn
endsql2005
create function [dbo].[func_splitid]
(@str varchar(max),@split varchar(10))
returns @t table (c1 nvarchar(100))
asbegin
declare @x xml
set @x = convert(xml,'')
insert into @t select x.item.value('@id[1]', 'nvarchar(100)') from @x.nodes('//items/item') as x(item)
return
end本文標題: sql中實現split函式幾種方法總結(必看篇)
本文位址:
split 函式實現
split函式實現 ss axx bv ctt dff result def split 1 ss,a,times len ss i 0n 0 while len ss 0 and iand nif ss i i len a a print i i print 之前的ss ss print resu...
使用SQL語句實現SPLIT效果的幾種方法
create table table1 id int identity 1,1 not null name char 10 collate chinese prc ci as null constraint pk table1 primary key clustered id on primary ...
oracle實現split函式
oracle資料庫中某乙個字段可能存在以某些特殊符號隔開的字段,我們在查詢使用的時候往往需要將這些欄位spilt開 但是oracle沒有這個函式,網上搜尋了一下,找了乙個可以使用的函式 下面直接上指令碼 1.先建立乙個type create or replace type obj target as...