请问
declare @str varchar(8000)
set @str = 'asd,asdf,asddff,asda,abc,cba,nba,cba '
while charindex( ', ',@str)!=0
begin
select left(@str, charindex( ', ',@str)-1)
set @str=right(@str,len(@str)-charindex( ', ',@str))
end
这样取不到最后一个cba
只能把前面的所有取出来
请问怎么才能取出所有?
------解决方案--------------------declare @str varchar(8000)
set @str = 'asd,asdf, '
while charindex( ', ',@str)!=0
begin
select left(@str, charindex( ', ',@str)-1)
set @str=right(@str,len(@str)-charindex( ', ',@str))
end
------解决方案--------------------1.
declare @str varchar(8000)
set @str = 'asd,asdf,asddff,asda,abc,cba,nba,cba '
while charindex( ', ',@str)> 0
begin
select left(@str, charindex( ', ',@str)-1)
set @str=right(@str,len(@str)-charindex( ', ',@str))
end
select @str
2.
declare @str varchar(8000)
set @str = 'asd,asdf,asddff,asda,abc,cba,nba,cba '
set @str=@str+ ', '
while charindex( ', ',@str)> 0
begin
select left(@str, charindex( ', ',@str)-1)
set @str=right(@str,len(@str)-charindex( ', ',@str))
end
------解决方案--------------------最后加个select @str
declare @str varchar(8000)
set @str = 'asd,asdf,asddff,asda,abc,cba,nba,cba '
while charindex( ', ',@str)!=0
begin
print left(@str, charindex( ', ',@str)-1)
set @str=right(@str,len(@str)-charindex( ', ',@str))
end
print @str
/*
我用的是print
asd
asdf
asddff
asda
abc
cba
nba
cba
*/
------解决方案--------------------3.如果是簡單的處理
declare @str varchar(8000)
set @str = 'asd,asdf,asddff,asda,abc,cba,nba,cba '
declare @sql varchar(8000)
set @sql= 'select ' ' '+replace(@str, ', ', ' ' ' union all select ' ' ')+ ' ' ' '
exec( @sql )
/*
------
asd
asdf
asddff
asda
abc
cba
nba
cba
*/