ALTER function [dbo].[f_split](@SourceSql varchar(8000),@StrSeprate varchar(10))
returns @temp table(a varchar(100))
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
returns @temp table(a varchar(100))
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
如果反过来,将多条记录合并到一个字段,可以用以下方法:
declare @var varchar(2000)
set @var=''
select @var=@var+cast(email as varchar(2000))+',' from users where user_classid=8
if len(@var)>0
set @var=left(@var,len(@var)-1)
else
set @var=''
select @var
set @var=''
select @var=@var+cast(email as varchar(2000))+',' from users where user_classid=8
if len(@var)>0
set @var=left(@var,len(@var)-1)
else
set @var=''
select @var