2012-12-10 90 views
-1

我有以下查詢:在日期問題之間

SELECT QuoteReference, CreatedDate, StartDate, EndDate, Operation, OccurredAt, PerformedBy, FieldName, OldValue, NewValue 
FROM Quotes, Audit 
WHERE Quotes.ID = Audit.RowId 
AND PaymentReference is not null 

AND Audit.OccurredAt > Quotes.CompletedDate 
AND Quotes.CreatedDate between '2010-04-01 11:00:00.027' AND '2010-07-30 11:39:22.027' 
and TableName = 'QUOTE' OR TableName = 'Quotes' 
ORDER BY Audit.OccurredAt desc 

儘管嘗試了一些事情,關於「quotes.createddate ......之間」行,我不能過濾的結果集,包括記錄創建日期爲2010年4月1日至2012年7月30日。我怎樣才能做到這一點?

回答

0

使用ISO DATETIME FORMAT (yyyymmdd or yyyy-mm-ddThh:mi:ss.mmm)排序的date formatting issue如有。也可嘗試USE JOIN表而不是WHERE表1,表2,..

如果其他條件都好了,下面的查詢應攜帶的記錄。

SELECT QuoteReference, CreatedDate, StartDate, EndDate, Operation, 
     OccurredAt, PerformedBy, FieldName, OldValue, NewValue 

FROM Quotes JOIN Audit ON Quotes.ID = Audit.RowId --JOIN TABLES 
WHERE PaymentReference is not null 
     AND Audit.OccurredAt > Quotes.CompletedDate 
     AND Quotes.CreatedDate 
      BETWEEN '20100401' AND '20100730' --Time ignored purposely for checking 
     AND TableName IN ('Quote','Quotes') 
ORDER BY Audit.OccurredAt desc 
+0

問題是查詢返回結果的結束日期創建日期之後。 :( – dotnetdev

+0

邏輯是好的,它可以用一些數據是別的東西,你能給的[SQL小提琴(http://sqlfiddle.com/#!3)? – Kaf

+0

我已經更新了查詢'無TIME',看?如果不進行檢查的目的什麼好的 – Kaf

0

您的日期範圍是不正確,使用方法:

between '2010-04-01 00:00:00:000' AND '2012-07-30 23:59:59:999' 
+0

問題是查詢返回結果的結束日期創建日期之後。:( – dotnetdev

+0

結局是什麼日期?「2012-07-30 23:59:59 :999' 或 '2012-07-30 00:00:00:000' –

+0

在你的答案,所以2012-07-30 23:59:59:999 – dotnetdev