各位朋友,SQL库里面的数据如图1,现在当用户进行排序操作后,希望DBGRIDEH表格可以显示出如图2的排序方法。图1:批号       牌号
7-06004        2-3
7-06004        10-11-12
7-06004        4-5
7-06004        112-113
7-06004        1
7-06004        6-7-8
7-06004        9
请问能否将以上的数据,按牌号里面,各个数字的大小来排序,最后的顺序如下图2:批号            牌号
7-06004         1
7-06004         2-3
7-06004         4--5
7-06004         6-7-8
7-06004         9
7-06004         10-11-12
7-06004         112-113请各位朋友赐教,多谢!

解决方案 »

  1.   

    select * from xxxx
    order by [牌号]
      

  2.   

    select * from a order by convert(int,case charindex('-',dt1) when 0 then dt1 
    else substring(dt1,0,charindex('-',dt1)) end) 
    试试
      

  3.   

    测试
    create table a (dt varchar(50),dt1 varchar(50))
    insert a select '7-06004','1'
    union all select '7-06004','10-11-12'
    union all select '7-06004','112-113'
    union all select '7-06004','2-3'
    union all select '7-06004','4-5'
    union all select '7-06004','9'
    union all select '7-06004','6-7-8'
    goselect * from a order by convert(int,case charindex('-',dt1) when 0 then dt1 
    else substring(dt1,0,charindex('-',dt1)) end) drop table a ----结果
    dt       dt1
    7-06004 1
    7-06004 2-3
    7-06004 4-5
    7-06004 6-7-8
    7-06004 9
    7-06004 10-11-12
    7-06004 112-113
      

  4.   

    首先感谢上面的各位朋友的热心回复,语句如下基本可以通过
    select kwph, ph from zzbfkb  
    order by
    case  when charindex('-',ph)>0 and  isnumeric(substring(ph,1,charindex('-',ph)-1))=1  then cast(substring(ph,1,charindex('-',ph)-1) as int)  
          when charindex('-',ph)>0 and  isnumeric(substring(ph,1,charindex('-',ph)-1))=0  then substring(ph,1,charindex('-',ph)-1)
          when charindex('-',ph)=0 and isnumeric(ph)=1 then cast(ph as int)
    else cast(substring(ph,1,len(ph)) as varchar) end
    但当遇到这样的记录,系统就提示出错了,还望各位朋友继续赐教,多谢!!!
    货物批号  牌号
    7-03009  120A
    服务器: 消息 245,级别 16,状态 1,行 1
    将 varchar 值 '120A' 转换为数据类型为 int 的列时发生语法错误。
      

  5.   

    呵呵~~~真是笨死了,明明上面已经有现成的答案了,自己还要走弯路,
    taxpayer大哥,衷心的谢谢您!!!现在问题终于解决了,下面的语句是从你的语句转化过来的
    select kwph, ph from zzbfkb order by len(convert(varchar(50),case charindex('-',ph) when 0 then ph 
    else substring(ph,0,charindex('-',ph)) end)),
    convert(varchar(50),case charindex('-',ph) when 0 then ph 
    else substring(ph,0,charindex('-',ph)) end)