日期:2014-05-17 浏览次数:20623 次
if object_id('Tempdb..#a') is not null drop table #a
--建临时表
create table #a(
ID int identity(1,1) not null,
pdate datetime null
)
--建示例数据
declare @i int
declare @n int
declare @date datetime
declare @time nvarchar(9)
set @i=1
set @n=1000
set @date='2013-06-25 1:0:0'
set @time=' 03:00:00'
while @i<=@n
begin
insert into #a select dateadd(hh,-1*@i,@date)
set @i=@i+1
end
--查询
;with cte as(
select *,datediff(dd,pdate,@date) days,cast(left(pdate,10)+@time as datetime) theTime from #a
)
select * from cte where days %7=0 and datediff(hh,pdate,theTime)=0
---------------------------
-----查询结果
ID pdate days theTime
----------- ----------------------- ----------- -----------------------
166 2013-06-18 03:00:00.000 7 2013-06-18 03:00:00.000