order by cast(数据 AS datetime)

解决方案 »

  1.   

    [code=SQL]order by cast(col as datetime)[/code]
      

  2.   

    order by cast (字段名 as datetime)
      

  3.   

    select *
    from 表名
    order by convert((CONVERT (varchar,日期,120)),datetime)
      

  4.   


    --> 测试数据:[tab]
    --> 测试时间:2009-07-06 17:08:06
    --> AD.: http://shop36766744.taobao.com/if object_id('[tab]') is not null drop table [tab]
    create table [tab]([t] varchar(10))
    insert [tab]
    select '05/08/2008' union all
    select '30/06/2008' union all
    select '25/02/2008'select t from tab 
    order by right(t,4)+substring(t,charindex('/',t)+1,2)+left(t,2)
    /*
    t          
    ---------- 
    25/02/2008
    30/06/2008
    05/08/2008(所影响的行数为 3 行)*/
    drop table tab
      

  5.   

    order by
      right(col,4),
      right(left(co,5),2),
      left(col,2)
      

  6.   

    -- =========================================
    -- -----------t_mac 小编-------------
       ---希望有天成为大虾---- 
    -- =========================================IF OBJECT_ID('tb') IS NOT NULL
      DROP TABLE tb
    GO
    CREATE TABLE tb( times char(10))
    go
    insert into tb
    select '05/08/2008' union all
    select '30/06/2008' union all
    select '25/02/2008'
    go
    select *
    from tb
    order by CONVERT(datetime,times,103)
    /*------------
    25/02/2008
    30/06/2008
    05/08/2008
    -------*/
      

  7.   


    declare @t table(col1 char(10))
    insert into @tselect '05/08/2008' union all
    select '30/06/2008' union all
    select '25/02/2008'
    select col1 from @t order by cast(right(col1,4)+'-'+substring(col1,4,2)+'-'+left(col1,2) as datetime)
    --这样就可以了
      

  8.   

    -- =========================================
    -- -----------t_mac 小编-------------
       ---希望有天成为大虾---- 
    -- =========================================IF OBJECT_ID('tb') IS NOT NULL
      DROP TABLE tb
    GO
    CREATE TABLE tb( times char(10))
    go
    insert into tb
    select '05/08/2008' union all
    select '30/06/2008' union all
    select '25/02/2008'
    go
    select *
    from tb
    order by CONVERT(datetime,times,103)
    /*------------
    25/02/2008
    30/06/2008
    05/08/2008
    -------*/
    有些记录没有数据?
      

  9.   

    --空的排后面
    order by isnull(cast(col as datetime),getdate())--空的排前面
    order by isnull(cast(col as datetime),'')