create FUNCTION [dbo].[f_splitstr]
(
-- Add the parameters for the function here
@SourceSql nvarchar(max),
@StrSeprate nvarchar(10)
)
RETURNS @temp table(F1 nvarchar(100))
AS
BEGIN
-- Fill the table variable with the rows for your result set
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
应用:select * from f_splitstr('1,2,3',',')