表中字段“日期”的内容为7个字符,如2011-6-,2011-11,请问如何截取月份并转换为数字用于升序排列,这里只截取6,10

解决方案 »

  1.   

    code=SQL]select  convert(char(7),getdate(),120)[/code]
      

  2.   

    为什么日期要存成2011-6-?--得到当前月份
    select month(getdate())
    /*
    6
    */
    --得到当前时间
    select getdate()
    /*
    2011-06-14 18:35:07.263
    */
    --只显示日期
    select convert(varchar(10),getdate(),120)
    /*
    2011-06-14
    */
    --只显示到月份
    select convert(varchar(7),getdate(),120)
    /*
    2011-06
    */
      

  3.   

    create table tb(dt varchar(7))
    insert into tb select '2011-6-' union all select '2011-10'
    go
    select month(case when right(dt,1)='-' then dt+'1' else dt+'-1' end)m from tb
    go
    drop table tb
    /*
    m
    -----------
    6
    10(2 行受影响)*/
      

  4.   


    不是标准日期了先转为日期类型,然后再去截取convert(varchar(7),convert(datetime,date),120)
      

  5.   

    select replace(right(zouqi,3),'-','') as m from qxzqsj