数字型的字符串字段排序问题

qufusongyu 2009-10-16 01:40:57
有一个字段为编号
内容大体如下:


1-1
1-2
......
1-50
....
50-5
50-6....
Select * from 表 order by 编号
排序不是按照我的意图来排的.
1和11都是挨着的了.
如果按照数字大小来排列呢
...全文
78 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
SQL77 2009-10-16
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 qufusongyu 的回复:]
这个字段里面如果含有Null值和空字符的话,这个语句会出错
[/Quote]
用CASE WHEN
Select * from 表 order by CASE WHEN 编号 IS NULL THEN 0 ELSE
substring(1,charindex('-',编号)-1) END asc
qufusongyu 2009-10-16
  • 打赏
  • 举报
回复
这个字段里面如果含有Null值和空字符的话,这个语句会出错
fwacky 2009-10-16
  • 打赏
  • 举报
回复
order by left(编号,charindex('-',编号)-1)
luoyoumou 2009-10-16
  • 打赏
  • 举报
回复
Select *  from 表 order by substring(1,charindex('-',编号)-1) asc,
substring(charindex('-',编号)+1,len(编号));
sgtzzc 2009-10-16
  • 打赏
  • 举报
回复
order by 
cast(left(编号,charindex('-',编号)-1) as int),
cast(right(编号,len(编号)-charindex('-',编号)) as int)
--小F-- 2009-10-16
  • 打赏
  • 举报
回复
---2楼正确 我掉了转换的一步
order by cast(left(编号,charindex('-',编号)-1) as int)
快乐_石头 2009-10-16
  • 打赏
  • 举报
回复
select * from tb order by cast(left(编号,charindex('-',编号)-1)as int)
sgtzzc 2009-10-16
  • 打赏
  • 举报
回复
order by cast(left(编号,charindex('-',编号)-1) as int)
--小F-- 2009-10-16
  • 打赏
  • 举报
回复
order by left(编号,charindex('-',编号)+1)

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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