Microsoft Access SQL查询没有年份的日期范围

时间:2017-01-20 23:48:21

标签: sql ms-access

我的问题类似于this question,但有点不同。

我有以下查询:

SELECT Cats_History.ID, Cats_History.TYPE, Cats_History.BEGDATE, Cats_History.ENDDATE, Cats_History.DESCRIPTION, Cats_History.LOSS, Cats_History.LOSSCURRENT
FROM Cats_History
WHERE (Month(Cats_History.BEGDATE)*100)+Day(Cats_History.BEGDATE) Between 101 And 131;

在SQL中指定月/日时,此方法正常。但是,如果我用[start]和[end]替换101和131,那么用户可以在单击我的表单按钮后输入值,结果不正确。

例如,如果我使用表单输入101和108,则会显示正确的1月日期,但也会显示10月日期。

SELECT Cats_History.ID, Cats_History.TYPE, Cats_History.BEGDATE, Cats_History.ENDDATE, Cats_History.DESCRIPTION, Cats_History.LOSS, Cats_History.LOSSCURRENT
FROM Cats_History
WHERE (Month(Cats_History.BEGDATE)*100)+Day(Cats_History.BEGDATE) Between [start] And [end];

如果我尝试除1月以外的任何日期,它似乎也能正常工作。

2 个答案:

答案 0 :(得分:1)

应该如此简单:

SELECT Cats_History.ID, Cats_History.TYPE, Cats_History.BEGDATE
  , Cats_History.ENDDATE, Cats_History.DESCRIPTION, Cats_History.LOSS
  , Cats_History.LOSSCURRENT
FROM Cats_History
WHERE Month(Cats_History.BEGDATE) = 1
  AND Day(Cats_History.BEGDATE) BETWEEN ([start] % 100) AND ([end] % 100);

我不确定你为什么要在10月份获得约会。听起来用于生成[start][end]的数学可能存在错误。

我建议只使用日期值并将查询简化为:

SELECT Cats_History.ID, Cats_History.TYPE, Cats_History.BEGDATE
  , Cats_History.ENDDATE, Cats_History.DESCRIPTION, Cats_History.LOSS
  , Cats_History.LOSSCURRENT
FROM Cats_History
WHERE Cats_History.BEGDATE BETWEEN [start] AND [end];

答案 1 :(得分:0)

SELECT Cats_History.ID, Cats_History.TYPE, Cats_History.BEGDATE, Cats_History.ENDDATE, Cats_History.DESCRIPTION, Cats_History.LOSS, Cats_History.LOSSCURRENT
FROM Cats_History
WHERE (Month(Cats_History.BEGDATE)*100)+Day(Cats_History.BEGDATE) Between CInt([start]) and CInt([end]);