加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sqlserver实现字符串从小到大排序

发布时间:2020-12-12 13:42:27 所属栏目:MsSql教程 来源:网络整理
导读:--字符串排序declare @a int,@b int,@c int,@d varchar(max),@e varchar(max),@f varchar(max)set @a=1set @b=1set @d='bbdgegpoakfoa'set @c=LEN(@d)set @e=SUBSTRING(@d,1,1)set @f=''while @b@cbeginwhile @aLEN(@d)beginif @eSUBSTRING(@d,@a,1)set @e=S
--字符串排序
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,1)
set @a=@a+1
end
set @d=stuff(@d,charindex(@e,@d,1),'')
set @a=1
set @b=@b+1
set @f=@f+@e
set @e=SUBSTRING(@d,1)
end

结果:abbdefggkoop

AndyHang提供的更简便好理解的方法,通过表的order by

set? nocount? on declare? @ table? table (val? char (1)) @s? varchar (8000),@i? int @s= 'bbdgegpoakfoa' @i=1 while?@i<=LEN(@s) begin ???? insert? into? select? SUBSTRING (@s,@i,1) @i=@i+1 end ? '' select? @s=@s+val? from? order? by? val print?@s ? /* aabbdefggkoop */

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读