2013-05-02 151 views
1

我有以下SQL,SQL查詢小於或等於16小時30分鐘?

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分鐘如何指定小時和分鐘

+0

'senttime'列的數據類型是什麼? – 2013-05-02 10:55:44

回答

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 
+0

請參閱vishal說使用OR而不是AND – user960567 2013-05-02 10:12:23

+0

請檢查編輯答案。 – TechDo 2013-05-02 10:29:28