有一个字段为编号
内容大体如下:
1-1
1-2
......
1-50
....
50-5
50-6....
Select *  from 表 order by 编号
排序不是按照我的意图来排的.
1和11都是挨着的了.
如果按照数字大小来排列呢

解决方案 »

  1.   

    order by left(编号,charindex('-',编号)+1)
      

  2.   

    order by cast(left(编号,charindex('-',编号)-1) as int)
      

  3.   

    select * from tb order by cast(left(编号,charindex('-',编号)-1)as int)
      

  4.   

    ---2楼正确 我掉了转换的一步
    order by cast(left(编号,charindex('-',编号)-1) as int)
      

  5.   

    order by 
      cast(left(编号,charindex('-',编号)-1) as int),
      cast(right(编号,len(编号)-charindex('-',编号)) as int)
      

  6.   

    Select *  from 表 order by substring(1,charindex('-',编号)-1) asc,
                               substring(charindex('-',编号)+1,len(编号));
      

  7.   

     order by left(编号,charindex('-',编号)-1)
      

  8.   

    这个字段里面如果含有Null值和空字符的话,这个语句会出错
      

  9.   

    用CASE WHEN
    Select *  from 表 order by CASE WHEN 编号 IS NULL THEN 0 ELSE 
    substring(1,charindex('-',编号)-1) END asc