create table test(日期 datetime) insert test select '2007-6-1' union all select '2007-6-2' union all select '2007-6-3' union all select '2007-2-1' union all select '2007-2-2' union all select '2007-2-3'select 日期,天数=datediff(day,日期+'-01',dateadd(month,1,日期+'-01')) from ( select 日期=convert(char(7),日期,120) from test group by convert(char(7),日期,120) )a drop table test 日期 天数 ------- ----------- 2007-02 28 2007-06 30
insert test select '2007-6-1'
union all select '2007-6-2'
union all select '2007-6-3'
union all select '2007-2-1'
union all select '2007-2-2'
union all select '2007-2-3'select 日期,天数=datediff(day,日期+'-01',dateadd(month,1,日期+'-01')) from
(
select 日期=convert(char(7),日期,120)
from test
group by convert(char(7),日期,120)
)a
drop table test
日期 天数
------- -----------
2007-02 28
2007-06 30