SQL 数字和字母混合的字段怎么排序

TangDongYao 2017-02-28 09:46:36


我想要的结果是

7
8
9
84
6B
6C
6D
6E
...全文
1629 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
baidu_35289351 2017-02-28
  • 打赏
  • 举报
回复
if object_id('tempdb..#tb') is not null drop table #tb create table #tb(abc varchar(20)) insert into #tb select '99a' union all select '991' union all select '7' union all select '8' union all select '9' union all select '84' union all select '6B' union all select '6C' union all select '6D' union all select '99' union all select '999' union all select '1a21b2' union all select '12a1b2' union all select '12a1a0' union all select '12a1b1' union all select '6E' select * from #tb order by len(abc),isnumeric(abc) desc,abc ------------- abc 7 8 9 84 99 6B 6C 6D 6E 991 999 99a 12a1a0 12a1b1 12a1b2 1a21b2
baidu_35289351 2017-02-28
  • 打赏
  • 举报
回复
7 8 9 84 99 6B 6C 6D 6E 991 999 99a
昵称被占用了 2017-02-28
  • 打赏
  • 举报
回复
如果字母保证只有一位,而且在最后 order by case when isnumeric(bh) = 1 then CONVERT(int, bh) else CONVERT(int, left(bh,len(bh)-1)) end 否则,可能需要写个取出数字的函数
baidu_35289351 2017-02-28
  • 打赏
  • 举报
回复
借用1楼的思路 if object_id('tempdb..#tb') is not null drop table #tb create table #tb(abc varchar(20)) insert into #tb select '99a' union all select '991' union all select '7' union all select '8' union all select '9' union all select '84' union all select '6B' union all select '6C' union all select '6D' union all select '99' union all select '999' union all select '6E' select * from #tb order by len(abc),isnumeric(abc) desc,abc
TangDongYao 2017-02-28
  • 打赏
  • 举报
回复
引用 1 楼 wmxcn2000 的回复:

order by ISNUMERIC(col) desc, col


你的答案得到的是这样的:

应该9在最上面其次是84 、95 、999
昵称被占用了 2017-02-28
  • 打赏
  • 举报
回复
order by case when isnumeric(bh) = 1 then CONVERT(int, bh) else 999999999 end, bh
TangDongYao 2017-02-28
  • 打赏
  • 举报
回复
引用 7 楼 wmxcn2000 的回复:
[quote=引用 2 楼 TangDongYao 的回复:] 上面的答案不对啊。还是对一些数据没进行排序
不会的,你除了你列出来的数据外,还有什么样的数据? 另外,你再详细说一下规则;[/quote] 我想要的是数字俺大小排在最前面 ( 1 2 3 11 22 111 ),然后是含字母的数字在后面并且形式如这样: ( 1A 1B 11A 11C 111C 111D )
TangDongYao 2017-02-28
  • 打赏
  • 举报
回复
数据如: 1 2 5 10 100 1A 1B 1C 10A 20B 100A 200C
TangDongYao 2017-02-28
  • 打赏
  • 举报
回复

出现后的结果是这样的
卖水果的net 2017-02-28
  • 打赏
  • 举报
回复
引用 2 楼 TangDongYao 的回复:
上面的答案不对啊。还是对一些数据没进行排序
不会的,你除了你列出来的数据外,还有什么样的数据? 另外,你再详细说一下规则;
zbdzjx 2017-02-28
  • 打赏
  • 举报
回复
借用楼上的数据写一个,其中的9999999999,目的是要将带字母的排到最后,如果字段比较长,也可以多个加几9。
with t1(bh) as
(
select '6C'  union all 
select '6D'  union all 
select '6B'  union all 
select '6C'  union all 
select '6D'  union all 
select '6E'  union all 
select '7'  union all 
select '8'  union all 
select '84'  union all 
select '9'  
)
select * 
from t1
order by case when bh like '%[a-z]%' then 9999999999 else CONVERT(int, bh) end, bh
二月十六 2017-02-28
  • 打赏
  • 举报
回复
引用 4 楼 TangDongYao 的回复:
这个必须要制定top 吗?
我写的那种得加,可以想想其他方法。
TangDongYao 2017-02-28
  • 打赏
  • 举报
回复
这个必须要制定top 吗?
二月十六 2017-02-28
  • 打赏
  • 举报
回复
语句:
if object_ID('tempdb..#A') is not  null
drop table #A
Go
CREATE TABLE #A
(
ghksjjbh NVARCHAR(100)
)
Insert #A
select '6C' union all
select '6D' union all
select '6B' union all
select '6C' union all
select '6D' union all
select '6E' union all
select '7' union all
select '8' union all
select '84' union all
select '9'

--测试数据结束
SELECT *
FROM ( SELECT TOP 100
*
FROM #A
WHERE ISNUMERIC(ghksjjbh) = 1
ORDER BY CONVERT(INT, ghksjjbh)
) AS a
UNION ALL
SELECT *
FROM ( SELECT TOP 100
*
FROM #A
WHERE ISNUMERIC(ghksjjbh) = 0
ORDER BY ghksjjbh
) AS b




结果:




TangDongYao 2017-02-28
  • 打赏
  • 举报
回复
上面的答案不对啊。还是对一些数据没进行排序
卖水果的net 2017-02-28
  • 打赏
  • 举报
回复

order by ISNUMERIC(col) desc, col
baidu_35289351 2017-02-28
  • 打赏
  • 举报
回复
引用 17 楼 TangDongYao 的回复:
[quote=引用 15 楼 baidu_35289351 的回复:] 7 8 9 84 99 6B 6C 6D 6E 991 999 99a
我怎么在这个函数的情况下去重复数据?我用distinct不行[/quote] select distinct 字段1,字段2,字段3,字段N from 你表
0与1之间 2017-02-28
  • 打赏
  • 举报
回复

With T(Name) As
(
select '2' union all
select '33' union all
select '8' union all
select '33' union all
select '8' union all
select '45' union all
select '96' union all
select '84' union all
select '6B' union all
select '7C' union all
select '65D' union all
select '9a' union all
select '999' 
)
Select *,case when isnumeric(name)=1 then 0 else 1 end as t from T group by Name
order by t asc,len(name) asc,name asc
TangDongYao 2017-02-28
  • 打赏
  • 举报
回复
引用 15 楼 baidu_35289351 的回复:
7 8 9 84 99 6B 6C 6D 6E 991 999 99a
我怎么在这个函数的情况下去重复数据?我用distinct不行

590

社区成员

发帖
与我相关
我的任务
社区描述
提出问题
其他 技术论坛(原bbs)
社区管理员
  • community_281
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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