昨天写了一个【字符串排序】,感觉有点挫
declare @a int, @b int, @c int, @d varchar(max), @e varchar(max), @f varchar(max)
set @a=1
set @b=1
set @d='bbdgegpoakfoa'
set @c=LEN(@d)
set @e=SUBSTRING(@d,1,1)
set @f=''
while @b<@c
begin
while @a<LEN(@d)
begin
if @e>SUBSTRING(@d,@a,1)
set @e=SUBSTRING(@d,@a,1)
set @a=@a+1
end
set @d=stuff(@d,charindex(@e,@d,1),1,'')
set @a=1
set @b=@b+1
set @f=@f+@e
set @e=SUBSTRING(@d,1,1)
end
print @f
有更好的方法吗
[解决办法]
set nocount on
declare @table table(val char(1))
declare @s varchar(8000),@i int
set @s='bbdgegpoakfoa'
set @i=1
while @i<=LEN(@s)
begin
insert into @table select SUBSTRING(@s,@i,1)
set @i=@i+1
end
set @s=''
select @s=@s+val from @table order by val
print @s
/*
aabbdefggkoop
*/
declare @a int,@d varchar(max),@f varchar(max)
declare @x table(v varchar(1))
set @d='bbdgegpoakfoa'
select @a=1
while(@a<=len(@d))
begin
insert into @x(v) values(substring(@d,@a,1))
select @a=@a+1
end
select @f=isnull(@f,'')+v from @x order by v
select @f 'f'
/*
f
-------------------------
aabbdefggkoop
(1 row(s) affected)
*/