34,837
社区成员




select *, ltrim(ID)+replicate('9',100-len(id)) from #T order by ltrim(ID)+replicate('9',100-len(id))
--> 测试数据: #T
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (ID varchar(8),数据 int)
insert into #T
select '1528',879 union all
select '152801',105 union all
select '15280101',12 union all
select '15280102',30 union all
select '15280103',24 union all
select '1529',1421 union all
select '152901',154 union all
select '15290101',22 union all
select '15290102',24
select * from #T order by ltrim(ID)+replicate('9',100-len(id))
/*
ID 数据
-------- -----------
15280101 12
15280102 30
15280103 24
152801 105
1528 879
15290101 22
15290102 24
152901 154
1529 1421
*/
create table ta(ID int,d int)
insert ta select
1528 , 879 union select
152801 , 105 union select
15280101 , 12 union select
15280102 , 30 union select
15280103 , 24 union select
1529 , 1421 union select
152901 , 154 union select
15290101 , 22 union select
15290102 , 24
go
select *
from ta
order by left(id,4) ,len(id) desc
drop table ta
/*
ID d
----------- -----------
15280101 12
15280102 30
15280103 24
152801 105
1528 879
15290101 22
15290102 24
152901 154
1529 1421
(所影响的行数为 9 行)
*/