sql字符串排序
时间:2010-11-29 来源:butterinsect
OrderChar(‘database’)=aaabdest
GroupChar(‘datadata’)={(a,4),(d,2),(t,2)}
代码 --冒泡排序
if object_id('OrderChar') is not null
drop function OrderChar
create function OrderChar(@string varchar(256))
returns varchar(256)
as
begin
declare @out varchar(256)
declare @length int
declare @i int
declare @j int
declare @chtmp char(1)
declare @ch1 char(1)
declare @ch2 char(1)
set @length = len(@string)
set @i = 1
while @i<=@length
begin
set @j=@length
while @j>@i
begin
select @ch1 = substring(@string, @j, 1)
select @ch2 = substring(@string, @j-1, 1)
if @ch1<@ch2
begin
set @chtmp = @ch1
set @ch1 = @ch2
set @ch2 = @chtmp
set @string =stuff(@string, @j-1, 1, @ch2)
set @string =stuff(@string, @j, 1, @ch1)
end
set @j=@j-1
end
set @i=@i+1
end
return @string
end
--冒泡排序结束-
相关阅读 更多 +