if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].SF_Split') and xtype in (N'FN', N'IF', N'TF')) begin drop function SF_Split end go /* created on 03/11/2011,jonse 调用例子如下: select * from dbo.[SF_Split]('abc-def-ghi-jkl','-') select * from dbo.[SF_Split]('共存媒体1,共存媒体2,共存媒体4',',') */ create function SF_Split(@SourceSql varchar(8000),@StrSeprate varchar(1)) returns @temp table(Short varchar(500)) --实现split功能 的函数 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) end if @SourceSql<>'\' insert @temp values(@SourceSql) return end
查询结果:
-----------------------------------------------
Short
------------------
共存媒体1
共存媒体2
共存媒体4
(3 行受影响)
-----------------------------------------------
说明:
sql自定义函数:SF_Split,根据指定的分割符,把一个字符串分割成若干条短数据
此函数适用于 sql server 2008