表A,字段:date1 值.2008-11-25 (遍历表A时间记录)
表B,字段:date2 值2008-11-25 12:39:27
值2008-11-24 22:39:27
值2008-11-24 12:39:27
希望实现:表A,date1字段的时间 既;2008-11-25
获取,该字段相应表B,大于此时间前一天的 20点之后,并且是当天12点之前,插入的数据,,,
即以上表b只有第二条满足,即:2008-11-24 22:39:27
这该如何实现。!!!!!!!!!!!!
表B,字段:date2 值2008-11-25 12:39:27
值2008-11-24 22:39:27
值2008-11-24 12:39:27
希望实现:表A,date1字段的时间 既;2008-11-25
获取,该字段相应表B,大于此时间前一天的 20点之后,并且是当天12点之前,插入的数据,,,
即以上表b只有第二条满足,即:2008-11-24 22:39:27
这该如何实现。!!!!!!!!!!!!
where
date2>convert(datetime, convert(varchar, dateadd(day,-1,'2008-11-25') ,101) +' 20:00:00')
and
date2<convert(datetime, convert(varchar, '2008-11-25' ,101) +' 12:00:00')
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)
http://feiyun0112.cnblogs.com/
select @tt = date1 from A select * from b
where
date2>convert(datetime, convert(varchar, dateadd(day,-1,@tt) ,101) +' 20:00:00')
and
date2 <convert(datetime, convert(varchar, @tt,101) +' 12:00:00')
提示convert不能用!!!!!
date1 datetime
)CREATE TABLE B (
date2 datetime
)GOINSERT INTO A VALUES('2008-11-25')INSERT INTO B VALUES('2008-11-25 11:39:27')
INSERT INTO B VALUES('2008-11-25 12:39:27')
INSERT INTO B VALUES('2008-11-24 22:39:27')
INSERT INTO B VALUES('2008-11-24 12:39:27')select ta.date1 dA,tb.date2 dB
from a ta left outer join b tb on datediff(day,ta.date1,tb.date2)=-1
where
datepart(hh,tb.date2)>20 and ta.date1=(
select top 1 date1 from a order by date1 desc
)
order by date2 ascDROP TABLE A
DROP TABLE B
在一群里找到一可用语句...SELECT date2
FROM a, b
WHERE DATEDIFF('h',date1,date2)>=-4 And DATEDIFF('n',date1,date2)<=720