排序问题!!!

wfy0089 2003-08-11 09:47:10
有一个序号字段,是字符串类型的,
1.1 1.1
1.1.1 1.1.1
1.1.10 1.1.2
1.1.2 1.1.10
这是现在的结果 这是我想要得到的结果.
应该怎么写排序呀????
...全文
44 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
97866 2003-08-11
  • 打赏
  • 举报
回复
select * from 表 order by cast('0.'+replace(字段名,'.','0') as decimal(9,18))
97866 2003-08-11
  • 打赏
  • 举报
回复
Select * from 表 Order by (len(字段名)-len(replace(字段名,'.','')))/len('.'),Len(字段名)
wfy0089 2003-08-11
  • 打赏
  • 举报
回复
myflok(阿棋) :这样不行,因为还有其它数据,
你的结果是这样的: 我要的是这样的:
1 1
2 1.1
1.1 1.1.1
1.2 1.1.10
2.1 1.2
2.2 2
1.1.1 2.1
1.1.10 2.2
.... ....
愉快的登山者 2003-08-11
  • 打赏
  • 举报
回复
修改大力函数:
drop function getorder
create function getorder(@field varchar(1000))
returns numeric(38)
as
begin
declare @bb int,@cc int,@value varchar(8000)
set @field = @field +'.' //。。。。。。。。。。。
select @bb=0,@cc=charindex('.',@field)
set @value=right(replicate('0',3)+substring(@field,@bb,@cc-@bb),3)
if @value='' return left(@field+replicate('0',38),38)
while @cc>0
begin
set @bb=@cc+1
set @cc=charindex('.',@field,@bb) //。。。。。。。。。。。
set @value=@value+right(replicate('0',3)+cast(substring(@field,@bb,case when @cc>0 then @cc-@bb else len(@field) end) as varchar),3)
end
return cast(left(@value+replicate('0',38),38) as numeric(38))
end

--测试:
declare @ table (a varchar(100))
insert @ values('1')
insert @ values('2')
insert @ values('1.1')
insert @ values('2.1.44.1.2.34')
insert @ values('2.1.45.1.1')
insert @ values('2.1')
insert @ values('3.2')

select * from @ order by dbo.getorder(a)

pengdali 2003-08-11
  • 打赏
  • 举报
回复
长度呀,小姐。
愉快的登山者 2003-08-11
  • 打赏
  • 举报
回复
修改大力函数:
drop function getorder
create function getorder(@field varchar(1000))
returns numeric(38)
as
begin
declare @bb int,@cc int,@value varchar(8000)
set @field = @field +'.' //。。。。。。。。。。。
select @bb=0,@cc=charindex('.',@field)
set @value=right(replicate('0',3)+substring(@field,@bb,@cc-@bb),3)
if @value='' return left(@field+replicate('0',38),38)
while @cc>0
begin
set @bb=@cc+1
set @cc=charindex('.',@field,@bb) //。。。。。。。。。。。
set @value=@value+right(replicate('0',3)+cast(substring(@field,@bb,case when @cc>0 then @cc-@bb else len(@field) end) as varchar),3)
end
return cast(left(@value+replicate('0',38),38) as numeric(38))
end

--测试:
declare @ table (a varchar(100))
insert @ values('1')
insert @ values('2')
insert @ values('1.1')
insert @ values('2.1.44.1.2.34')
insert @ values('2.1.45.1.1')
insert @ values('2.1')
insert @ values('3.2')

select * from @ order by dbo.getorder(a)

dafu71 2003-08-11
  • 打赏
  • 举报
回复
decimal[(p[, s])] 和 numeric[(p[, s])]

定点精度和小数位数。使用最大精度时,有效值从 - 10^38 +1 到 10^38 - 1。decimal 的 SQL-92 同义词是 dec 和 dec(p, s)。

p(精度)

指定小数点左边和右边可以存储的十进制数字的最大个数。精度必须是从 1 到最大精度之间的值。最大精度为 38。

dafu71 2003-08-11
  • 打赏
  • 举报
回复
declare @a table (a varchar(100))
insert @a values ('1.1')
insert @a values ('1.1.1')
insert @a values ('1.1.10')
insert @a values ('1.1.2')

select * from @a order by cast(replace(a,'.','0') as numeric(38,0))
wfy0089 2003-08-11
  • 打赏
  • 举报
回复
38代表什么参数呀?
快来!!!
wfy0089 2003-08-11
  • 打赏
  • 举报
回复
大力:
returns numeric(38)这句是什么意思?
pengdali 2003-08-11
  • 打赏
  • 举报
回复
create function getorder(@field varchar(1000))
returns numeric(38)
as
begin
declare @bb int,@cc int,@value varchar(8000)
select @bb=0,@cc=charindex('.',@field)
set @value=right(replicate('0',3)+substring(@field,@bb,@cc-@bb),3)
if @value='' return left(@field+replicate('0',38),38)
while @cc>0
begin
set @bb=@cc+1
set @cc=charindex('-',@field,@bb)
set @value=@value+right(replicate('0',3)+cast(substring(@field,@bb,case when @cc>0 then @cc-@bb else len(@field) end) as varchar),3)
end
return cast(left(@value+replicate('0',38),38) as numeric(38))
end

--测试:
declare @ table (a varchar(100))
insert @ values('1')
insert @ values('1.1')
insert @ values('2.1.44.1.2.34')
insert @ values('2.1.45.1.1')
insert @ values('2.1')
insert @ values('3.2')

select * from @ order by dbo.getorder(a)

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧