日期:2014-05-17 浏览次数:20711 次
create table tb(tm datetime)
insert into tb values('2013-07-16 08:00:00')
insert into tb values('2013-06-01 08:00:00')
insert into tb values('2013-06-01 08:02:00')
insert into tb values('2013-06-30 07:00:00')
insert into tb values('2013-06-30 09:00:00')
select m.tm m_tm, n_tm = (select top 1 * from tb n where datediff(dd , m.tm , n.tm) = 0 and n.tm >= m.tm order by n.tm)
from
(
select convert(varchar(8),dateadd(mm,-1,getdate()),120)+'01 08:00:00' tm union
select dateadd(dd,-1,convert(varchar(8),getdate(),120)+'01 08:00:00') tm
) m
drop table tb
/*
m_tm n_tm
----------------------- -----------------------
2013-06-01 08:00:00.000 2013-06-01 08:00:00.000