数据库中有一字段 “编号B”,是以时间条件按年月产生的序列号,形如ID  编号B1   2012-08-68
2   2012-06-36
3   2012-12-1002(12后面1002这个数字,最多9999)
4   2012-12-985
我想实现以下这样的排序方法
基础写法:SELECT 编号B ORDER BY 编号B DESC
这样排序后会有一个问题2012-12-1002
会在
2012-12-985
前面----------------------
现在我想把排序条件由 2012-12-1002 变为 2012121002 ,同时转化为整数,进行排序,这 sql 语句要怎么写呢?谢谢 

解决方案 »

  1.   

    SELECT 编号B ORDER BY replace(编号B,'-','')*1 DESC
      

  2.   

    SELECT 编号B ORDER BY REPLACE(编号B,'-','')*1 DESC
    正解!
      

  3.   

    SELECT 编号B ORDER BY CAST(replace(编号B,'-','')AS INT) DESC
      

  4.   


    declare @T table([ID] int,[编号B] varchar(12))
    insert @T
    select 1,'2012-08-68' union all
    select 2,'2012-06-36' union all
    select 3,'2012-12-1002' union all
    select 4,'2012-12-985'select * from @T 
    ORDER BY LEFT([编号B],8),SUBSTRING([编号B],9,LEN([编号B])-8)*1 
    /*
    ID          编号B
    ----------- ------------
    2           2012-06-36
    1           2012-08-68
    4           2012-12-985
    3           2012-12-1002
    */