try:
SqlServer
select datepart(year,Ddate)+'-'+datepart(month,Ddate) from AA;
Oracle
select to_char(Ddate,"YYYY-MM") from AA;
SqlServer
select datepart(year,Ddate)+'-'+datepart(month,Ddate) from AA;
Oracle
select to_char(Ddate,"YYYY-MM") from AA;
select convert(varchar,datepart(year,Ddate))+'-'+convert(varchar,datepart(month,Ddate)) from AA;
是不正确的,类型是整型,不能和'-'计算select convert(char(7),日期,120)
得到的值包含time部分select distinct datepart(year,Ddate)+'-'+datepart(month,Ddate) from AA;
也是不正确的,类型是整型,不能和'-'计算
这个是正确的,我这可以的~
select distinct convert(varchar,datepart(year,Ddate))+'-'+convert(varchar,datepart(month,Ddate)) from AA;
已经测试过了,没有问题
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