例1
代码如下 | 复制代码 |
create function f_split(@SourceSql varchar(8000),@StrSeprate varchar(10)) select * from dbo.f_split('1,2,3,4',',') a (所影响的行数为 4 行) |
例2
代码如下 | 复制代码 |
--SQL Server Split函数 CREATE FUNCTION F_SQLSERVER_SPLIT(@Long_str varchar(8000),@split_str varchar(100)) |
例3
Sql2000andSql2005实用的Split函数
代码如下 | 复制代码 |
sql2000 CREATE FUNCTION [dbo].[splitstring_array] ( @string nvarchar(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)) end else if charindex(@split,@string,1) = 0 begin set @v_code = @string set @string = '' end insert into @array(onestr) values(@v_code) end RETURN END sql2005 CREATE function [dbo].[func_splitid] (@str varchar(max),@split varchar(10)) RETURNS @t Table (c1 nvarchar(100)) AS BEGIN 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 |