try:
SqlServer
select datepart(year,Ddate)+'-'+datepart(month,Ddate) from AA;
Oracle
select to_char(Ddate,"YYYY-MM") from AA;

解决方案 »

  1.   

    select id, year(ddate) + '-' + month(ddate) from AA
      

  2.   

    select convert(char(7),日期,120)
      

  3.   

    select distinct datepart(year,Ddate)+'-'+datepart(month,Ddate) from AA;
      

  4.   

    SqlServer
    select convert(varchar,datepart(year,Ddate))+'-'+convert(varchar,datepart(month,Ddate)) from AA;
      

  5.   

    select distinct (substring(Ddate,0,6)) from AA order by (substring(Ddate,0,6))
      

  6.   

    select distinct convert(varchar(7),ddate,120) from tablename
      

  7.   

    select id, year(ddate) + '-' + month(ddate) from AA
    是不正确的,类型是整型,不能和'-'计算select convert(char(7),日期,120)
    得到的值包含time部分select distinct datepart(year,Ddate)+'-'+datepart(month,Ddate) from AA;
    也是不正确的,类型是整型,不能和'-'计算
      

  8.   

    select distinct convert(varchar(7),ddate,120) from tablename
    这个是正确的,我这可以的~
      

  9.   

    SqlServer
    select distinct convert(varchar,datepart(year,Ddate))+'-'+convert(varchar,datepart(month,Ddate)) from AA;
    已经测试过了,没有问题
      

  10.   

    我多加一條記錄create table tb(id int,Ddate datetime)
    Insert into tb 
    select '1','2001-1-1'
    union all select '2','2001-1-2'
    union all select '3','2001-2-1'
    union all select '4','2001-2-2'
    union all select '5','2001-2-3'
    union all select '6','2002-5-1'
    union all select '6','2002-10-1'select * from tb--刪除表
    drop table tbselect distinct Ddate=replace(left(convert(varchar(10),Ddate,120),7),'-0','-') from tb --結果
    2001-1
    2001-2
    2002-10
    2002-5