日期:2014-05-17 浏览次数:20487 次
declare @startime datetime
declare @endtime datetime
select @startime = '2012-11-05',
@endtime = '2013-2-15'
select convert(varchar(10),dateadd(day,-1,dateadd(month,1,convert(varchar(7),DATEADD(day,number,@startime),120)+'-01')),120) as date
from master..spt_values t
where type ='p' and t.number >=0 and
t.number <= DATEDIFF(DAY,@startime,@endtime)
group by convert(varchar(7),DATEADD(day,number,@startime),120)
/*
date
2012-11-30
2012-12-31
2013-01-31
2013-02-28
*/
/*
--创建函数
create function generateTime
(
@begin_date datetime,
@end_date datetime
)
returns @t table(date datetime)
as
begin
with maco as
(
select @begin_date AS date
union all
select date+1 from maco
where date+1 <=@end_date
)
insert into @t
select * from maco option(maxrecursion 0);
return
end
*/
DECLARE @startime DATE
SET @startime='2012-11-05'
DECLARE @endtime DATE
SET @endtime='2013-2-15'
SELECT DISTINCT CONVERT(DATE,DATEADD(DD,-1,DATEADD(MONTH,1+DATEDIFF(MONTH,0,[date]),0)))[月末]
FROM dbo.generateTime(@startime,@endtime)
/*
月末
----------
2012-11-30
2012-12-31
2013-01-31
2013-02-28
*/
declare @startime DATETIME,@endtime DATETIME,@i int
select @startime = '2012-11-05',@endtime &nbs