日期:2014-05-17  浏览次数:20485 次

sql对数字和字母组合字段根据数字排序
数据类似1,3,2H,100CD,12K,10 数字的长度不固定,想要的结果是1,2H,3,10,12K,100CD
------解决方案--------------------

--单纯的数字在前,字母在后是比较好处理的

create table T (col varchar(10))
insert into T values(1)
insert into T values(3)
insert into T values('2H')
insert into T values('100CD')
insert into T values('12K')
insert into T values(10)

select 
  col, 
  convert( int, 
  case when patindex('%[^0-9]%',col) >0
       then substring(col,1, patindex('%[^0-9]%',col) -1 )
       else col
       end
        ) as new_number
from T
order by 2

/**
col  new_number
1 1
2H 2
3 3
10 10
12K 12
100CD 100
**/
drop table T