在日期之间发行

时间:2012-12-10 12:47:28

标签: tsql

我有以下查询:

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日)。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

使用ISO DATETIME FORMAT (yyyymmdd or yyyy-mm-ddThh:mi:ss.mmm)date formatting issue (如果有)进行排序。也尝试USE JOIN表而不是WHERE table1,table2,..

如果其他条件没问题,以下查询应该带上记录。

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

答案 1 :(得分:0)

您的日期范围不正确,请使用:

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