查询的SQL小于或等于16小时30分钟?

时间:2013-05-02 10:05:18

标签: sql sql-server

我有以下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分钟如何指定小时和分钟

1 个答案:

答案 0 :(得分: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