表AA
----
ID
1-1
1-100
1-101
1-11
1-110
1-111
1-12
1-120
1-121排序结果ID
------
1-1
1-11
1-12
1-100
1-101
1-110
1-111
1-120
1-121
请教.

解决方案 »

  1.   

    select * from 表
    order by substring(ID,1,1),substring(ID,3,1),substring(ID,4,1),substring(ID,5,1)
      

  2.   

    declare @T table(ID varchar(10))
    insert into @t select '1-1'
    union all select '1-100'
    union all select '1-101'
    union all select '1-11'
    union all select '1-110'
    union all select '1-111'
    union all select '1-12'
    union all select '1-120'
    union all select '1-121'select * from @t order by left(ID,charindex('-',ID)-1),cast(substring(ID,charindex('-',ID)+1,len(ID)) as int)
      

  3.   

    group by convert(int,substring(id,3,len(id)))
      

  4.   

    select * from 表名 order by cast(left(id,1) as int),cast(right(id,len(id)-2) as int)
      

  5.   

    --保险点全转成int
    declare @T table(ID varchar(10))
    insert into @t select '1-1'
    union all select '1-100'
    union all select '1-101'
    union all select '1-11'
    union all select '1-110'
    union all select '1-111'
    union all select '1-12'
    union all select '1-120'
    union all select '1-121'select * from @t order by cast(left(ID,charindex('-',ID)-1) as int),cast(substring(ID,charindex('-',ID)+1,len(ID)) as int)
      

  6.   

    select * from 表
    order by cast(substring(ID,1,1) as int),cast(substring(ID,3,3) as int)
      

  7.   

    group by convert(int,left(id,charindex('-',id))),convert(int,substring(id,charindex('-',id)+1,len(id)))
      

  8.   

    Trydeclare @a table
    (id varchar(10)) insert into @a
    select '1-1'
    union all
    select '1-100'
    union all
    select '1-101'
    union all
    select '1-11'
    union all
    select '1-110'
    union all
    select '1-111'
    union all
    select '1-12'
    union all
    select '1-120'
    union all
    select '1-121'select * from @a
    order by right(id,len(id)-2)
      

  9.   

    发错了 -_-
    select * from @a
    order by cast(right(id,len(id)-2) as int)
    这个才是
      

  10.   

    group by convert(int,left(id,charindex('-',id)-1)),convert(int,substring(id,charindex('-',id)+1,len(id)))
      

  11.   

    晕 今天怎么老把order by 写成group by !
      

  12.   

    declare @T table(ID varchar(10))
    insert into @t select '1-1'
    union all select '1-100'
    union all select '1-101'
    union all select '1-11'
    union all select '1-110'
    union all select '1-111'
    union all select '1-12'
    union all select '1-120'
    union all select '1-121'select * from @T order by cast(right(ID,len(ID)-2) as int)