日期:2014-05-18  浏览次数:20435 次

如何对公交车次(1,2,3,11,20,K3,K4,T5,T6)排序呢?
开始我设计准备两个表搞联合,但是因为类型不一致。请教!!

------解决方案--------------------
create table t1(a int , b varchar(10))
insert t1
select 1, 'K1 '
union all
select 2, '4 '
union all
select 3, 'T8 '
union all
select 2, '1 '
union all
select 3, '2 '
union all
select 2, 'K4 '
union all
select 3, '3 '
union all
select 2, 'T1 '
union all
select 3, 'T4 '

SELECT * FROM T1 ORDER BY B
------解决方案--------------------
在上面楼上的基础上我做了修改,执行下面语句:
create table t1(a int , b varchar(10))
insert t1
select 1, 'K1 '
union all
select 2, '4 '
union all
select 3, 'T8 '
union all
select 2, '1 '
union all
select 3, '2 '
union all
select 2, 'K4 '
union all
select 3, '3 '
union all
select 2, 'T1 '
union all
select 3, 'T4 '

insert into t1 values(1, '11 ')

--查询脚本
declare @MaxLength int
select @MaxLength = max(len(b)) from t1

select * from t1 order by Replicate( '0 ',@MaxLength-len(b))+b
可以得到楼主要求的效果。


------解决方案--------------------
create table t1(a int , b varchar(10))
insert t1
select 1, 'K1 '
union all
select 2, '234 '
union all
select 3, 'T8 '
union all
select 2, '110 '
union all
select 3, '2 '
union all
select 2, 'K4 '
union all
select 3, '32 '
union all
select 2, 'T1 '
union all
select 3, 'T4 '


SELECT * FROM T1
ORDER BY case when ISNUMERIC(b) = 1 then cast(b as int) else cast(left(cast(ascii(upper(left(b,1))) as varchar(100))+right(b,len(b)-1)+ '00000000 ',8) as int) end