表中有个字段date,nvarchar类型,存储日期,日期有2种格式:
年月日的,如1998-5-6
年月的,如1986-11
要求按照按照date字段的年月排序,怎么写这个sql语句啊,请各位大侠指教

解决方案 »

  1.   

    select * from 表
    order by date desc
    不知道lz是不是这个意思
      

  2.   

    这样是不行的,因为date字段不是大特提么类型的,请看
    2006-5-25
    2006-12-2
    2006-12-2
    2005-6-1
    2005-6-1
    2005-3
    2001-3
    2000-6-1
    2000-1-1
    1985-5
      

  3.   

    上面写错了,date字段不是datetime类型
      

  4.   

    create table a (id varchar(10))insert into a select '2006-5-25'
    union all select '2006-12-2'
    union all select '2006-12-2'
    union all select '2005-6-1'
    union all select '2005-6-1'
    union all select '2005-3'
    union all select '2001-3'
    union all select '2000-6-1'
    union all select '2000-1-1'
    union all select '1985-5'select * from a 
    order by cast (id+(case when len(id)>7 then '' else '-01' end ) as datetime )id         
    ---------- 
    1985-5
    2000-1-1
    2000-6-1
    2001-3
    2005-3
    2005-6-1
    2005-6-1
    2006-5-25
    2006-12-2
    2006-12-2(所影响的行数为 10 行)