1

我有以下 SQL,

SELECT dims_branchcode, 
       dims_documentname, 
       dims_branchname, 
       Count(dims_documentname) AS NoofRequestWithinCutoff, 
       track.sentdate 
FROM   track 
       INNER JOIN td_wf_dims394 
               ON track.documentno = td_wf_dims394.sno 
WHERE  ( track.indexcardname = 'TD_WF_DIMS394' 
         AND track.sender = 0 
         AND Datepart(hh, senttime) <= 16 ) 
GROUP  BY dims_branchcode, 
          dims_branchname, 
          dims_documentname, 
          sentdate 
ORDER  BY track.sentdate 

这在 16 小时内工作得很好。但我需要 16 小时和 30 分钟来指定小时和分钟

4

1 回答 1

4

添加条件 990 为 (16*60) + 30 分钟

AND DATEDIFF(mi, DATEADD(dd, 0, DATEDIFF(dd, 0, senttime)), senttime)<=990

代替

AND Datepart(hh, senttime) <= 16

检查样本数据:

declare @tbl as table(dts datetime)

insert into @tbl values 
('2012-01-07 17:27'), 
('2012-01-07 16:27'), 
('2012-02-22 11:36')

select * From @tbl

--condition datepart(hh, dts)<=16 and datepart(minute, dts)<=30, which doesnot include the 3rd row
select * From @tbl where datepart(hh, dts)<=16 and datepart(minute, dts)<=30 

--gives the desired result
select * From @tbl where DATEDIFF(mi, DATEADD(dd, 0, DATEDIFF(dd, 0, dts)), dts)<=990
于 2013-05-02T10:10:29.280 回答