USE [AppCloud] GO /****** Object: UserDefinedFunction [dbo].[splitstr] Script Date: 12/19/2013 09:33:07 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO Create function [dbo].[splitstr](@SourceSql varchar(8000),@StrSeprate varchar(5)) returns @temp table(F1 varchar(2000)) 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 GO
调用方法
select
F1 from dbo.SplitStr('1,2,3',',')