我写的报告需要每天0900到1700之间收集数据。 我认为它会很好,如下:
cast(convert(char(8),t.trxtime,112)as time)
between CONVERT(VARCHAR(5),getdate(),108) >= '09:00'
and CONVERT(VARCHAR(5),getdate(),108) < '17:00'
....但是没有雪茄。
谢谢!!!
答案 0 :(得分:2)
嗯,你可以使用datepart()
:
where datepart(hour, t.trxtime) between 9 and 16 and
cast(t.trxtime as date) = cast(getdate() as date)
我不确定日期比较是否真的有必要。
答案 1 :(得分:0)
你可以这样做(假设你实际上是指当前日期,而不是范围内的每个日期:
declare @startDate datetime
declare @endDate datetime
select @startDate = '2014-11-03 09:00:00',
@endDate = '2014-11-03 17:00:00'
select *
from table
where myDate between @startDate and @endDate
如果你确实在每天的0900和1700之间做了意思,你可以这样做:
declare @startDate datetime
declare @endDate datetime
select @startDate = '2014-10-03',
@endDate = '2014-11-03' -- note i'm still limiting it to a range of ~1 month
select *
from table
where myDate between @startDate and @endDate
and datepart(hour, myDate) between 9 and 17