一个自定义Sql Server字符串分割函数split,类似C#里的split函数,功能虽然不是很强大,应付一般的问题足矣
函数定义
Create function [dbo].[split] ( @SourceSql varchar(max), @StrSeprate varchar(10) ) returns @temp table(line varchar(max)) as begin declare @i int set @SourceSql = rtrim(ltrim(@SourceSql)) set @i = charindex(@StrSeprate,@SourceSql) while @i >= 1 begin if len(left(@SourceSql,@i-1))>0 begin insert @temp values(left(@SourceSql,@i-1)) end set @SourceSql=substring(@SourceSql,@i+len(@StrSeprate),len(@SourceSql)-@i) set @i=charindex(@StrSeprate,@SourceSql) end if @SourceSql <> '' insert @temp values(@SourceSql) return end
—–测试
Select * From dbo.split('asd,f,gh,jk,l',',')