关于sql 排序204-1-1、204-1-11、204-1-2怎么排序成204-1-1、204-1-2、204-1-11

解决方案 »

  1.   

    转化成日期类型再排序吧order by cast(字段 as  datetime)
      

  2.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2014-06-03 11:25:56
    -- Version:
    --      Microsoft SQL Server 2012 - 11.0.2100.60 (Intel X86) 
    -- Feb 10 2012 19:13:17 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Edition: Core-based Licensing on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([col] varchar(8))
    insert [tb]
    select '204-1-1' union all
    select '204-1-11' union all
    select '204-1-2'
    --------------开始查询--------------------------
    select * from [tb] order by cast(replace(col,'-','') as int)
    ----------------结果----------------------------
    /* col
    --------
    204-1-1
    204-1-2
    204-1-11(3 行受影响)*/
      

  3.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2014-06-03 11:25:56
    -- Version:
    --      Microsoft SQL Server 2012 - 11.0.2100.60 (Intel X86) 
    -- Feb 10 2012 19:13:17 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Edition: Core-based Licensing on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([col] varchar(80))
    insert [tb]
    select '204-1-1' union all
    select '204-1-11' union all
    select '204-1-2' union all
    select '新204-1-12'
    --------------开始查询--------------------------
    select * from [tb] order by case when IsNumeric(replace(col,'-',''))=1 then 0 else 1 end   
    ----------------结果----------------------------
    /*col
    --------------------------------------------------------------------------------
    204-1-1
    204-1-11
    204-1-2
    新204-1-12(4 行受影响)*/
      

  4.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2014-06-03 11:25:56
    -- Version:
    --      Microsoft SQL Server 2012 - 11.0.2100.60 (Intel X86) 
    -- Feb 10 2012 19:13:17 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Edition: Core-based Licensing on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([col] varchar(80))
    insert [tb]
    select '204-1-1' union all
    select '204-1-11' union all
    select '204-1-2' union all
    select '新204-1-12'
    --------------开始查询--------------------------
    select 
     col
    from
    (select case when IsNumeric(replace(col,'-',''))=1 then cast(replace(col,'-','') as int) else 1000000000000000000 end as px,* from [tb])t
    order by
     px,col ----------------结果----------------------------
    /*col
    --------------------------------------------------------------------------------
    204-1-1
    204-1-2
    204-1-11
    新204-1-12
    */