为什么日期范围不起作用?
当输入值为日期时,它有效。当输入值是日期范围时,它不会返回任何内容。
Select CaseIncomingLetters.pk_CaseIncomingLetters_CaseIncomingLetterID, CaseIncomingLetters.IncomingLetterTo,
CaseIncomingLetters.IncomingLetterNo, CaseIncomingLetters.IncomingLetterFrom, CaseIncomingLetters.IncomingLetterEntryDateTime,
Convert(varchar, CaseIncomingLetters.IncomingLetterDate, 106) as IncomingLetterDate, CaseIncomingLetters.IncomingLetterCopyTitle, CaseIncomingLetters.IncomingLetterCopy,
CaseIncomingLetters.IncomingLetterComments, CaseIncomingLetters.IncomingLetterCC, CaseIncomingLetters.IncomingLetterBody,
CaseIncomingLetters.fk_WebUsers_CaseIncomingLetters_UserID, CaseIncomingLetters.fk_ConcernedOffices_CaseHearings_ConcernedOfficeID,
CaseIncomingLetters.fk_Cases_CaseIncomingLetters_CaseID, Cases.pk_Cases_CaseID, Cases.CaseNo, Cases.CaseTitle,
ConcernedOffices.ConcernedOfficeName
From CaseIncomingLetters
Inner Join ConcernedOffices
ON ConcernedOffices.pk_ConcernedOffices_ID= CaseIncomingLetters.fk_ConcernedOffices_CaseHearings_ConcernedOfficeID
Inner Join Cases
ON Cases.pk_Cases_CaseID= CaseIncomingLetters.fk_Cases_CaseIncomingLetters_CaseID
Inner Join Web_Users
ON Web_Users.UserID= CaseIncomingLetters.fk_WebUsers_CaseIncomingLetters_UserID
Inner Join Orgs
ON Orgs.pk_Orgs_OrgID= Web_Users.fk_Orgs_OrgID_WebUsers
Where Web_Users.fk_Orgs_OrgID_WebUsers= @TownID AND Cases.CaseNo like '%'+ @CaseNo +'%' AND
(CaseIncomingLetters.IncomingLetterDate >= @DateFrom
AND CaseIncomingLetters.IncomingLetterDate < DATEADD(day, 1, @DateTo))
输入值为DateTime类型。
更新
我尝试了这个并且它可以工作但不高于一个
/****** Script for SelectTopNRows command from SSMS ******/
SELECT TOP 1000 [pk_CaseIncomingLetters_CaseIncomingLetterID]
,[fk_Cases_CaseIncomingLetters_CaseID]
,[IncomingLetterNo]
,[IncomingLetterComments]
,[IncomingLetterFrom]
,[IncomingLetterTo]
,[IncomingLetterBody]
,[IncomingLetterDate]
,[fk_ConcernedOffices_CaseHearings_ConcernedOfficeID]
,[IncomingLetterCC]
,[IncomingLetterCopy]
,[IncomingLetterCopyTitle]
,[IncomingLetterEntryDateTime]
,[fk_WebUsers_CaseIncomingLetters_UserID]
FROM [LitMS_MCP].[dbo].[CaseIncomingLetters]
Inner Join Web_Users
ON Web_Users.UserID= CaseIncomingLetters.fk_WebUsers_CaseIncomingLetters_UserID
Inner Join Orgs
ON Orgs.pk_Orgs_OrgID= Web_Users.fk_Orgs_OrgID_WebUsers
Inner Join Cases
ON Cases.pk_Cases_CaseID= CaseIncomingLetters.fk_Cases_CaseIncomingLetters_CaseID
Inner Join ConcernedOffices
ON ConcernedOffices.pk_ConcernedOffices_ID= CaseIncomingLetters.fk_ConcernedOffices_CaseHearings_ConcernedOfficeID
Where Web_Users.fk_Orgs_OrgID_WebUsers= 1 AND Cases.CaseNo like '%'+ '' +'%'
AND
(CaseIncomingLetters.IncomingLetterDate >= '1/Jan/1900'
AND
CaseIncomingLetters.IncomingLetterDate < DATEADD(day, 1, '21/Jan/2016'))
答案 0 :(得分:0)
我会采取的措施。
首先注释掉where子句的日期部分,并可能暂时更改查询以选择前10名(如果这是一个大数据集,则减少查询时间。
现在您可以看到CaseIncomingLetters.IncomingLetterDate的实际值。
在where子句中添加2个日期子句中的第一个,并将变量更改为您在最初返回的其中一个记录中找到的实际日期。这可以确保您在变量中查找的数据是以后可以实际返回的测试。如果此时没有得到任何结果,我不得不怀疑表中的数据类型。
现在,将变量的值更改为该变量,并使where子句使用该变量。如果以不同于存储在数据库中的方式填充日期,则可能是在查看排序规则问题。
对第二个日期条款执行相同操作。
记录丢失的地方是您遇到问题的地方。