如何准确地selectA和B之间的date?

这个问题继续讨论从SQL结果是不正确的,因为SQLdate比较设置不正确

我在excel中使用ADO在Excel中运行SQL,并将结果显示在Excel工作表上。

工作表2014,2015,2016,2017中有一个名为“date”的字段

数据示例:2/1 / 2014,7 / 1 / 2014,23 / 10/2014

此字段数据types是在日/月/年,date由= DATE(cell1,cell2,cell3) – 年,月,日组合而成。 表中的date应该是纯粹的date,因为我将3个单元格(年,月,日)合并为1个字段(date)


现在,我想设置fromdate和todate之间的date范围(包括两者)

' This SQL select the date between date-VARIABLE: fromdate & toDate WHERE date >= #" & fromdate & "# AND date<#" & toDate & "#" 

完整的SQL在这里:

 SELECT officer ,NULL, SUM(IIF( isnumeric(mkt) = true and Survey='CPI' and Activity='FI' and Outcome= 'C', Totalmin, 0 )/468) , SUM(IIF( isnumeric(Non) = true and Survey='CPI' and Activity='FI' and Outcome= 'C', Totalmin, 0 )/468) ,NULL ,NULL , IIF(ISNULL(sum(mkt)),0,sum(mkt)),Sum(Non),sum(ICP),(sum(mkt)+Sum(Non)+sum(ICP) ) ,NULL,NULL,NULL,count(IIF( Survey='CPI' and Activity='FI' ,Totalmin, NULL )),NULL,count(IIF( Survey='CPI' and Activity='FI' and (Outcome ='C' OR Outcome='D'OR Outcome='O') , Totalmin, NULL )),NULL,SUM(IIF( Survey='CPI' and Activity='FI' ,Totalmin, 0 )),NULL,SUM(IIF( Survey='CPI' and Activity='FI' and (Outcome ='C' OR Outcome='D') ,Totalmin, 0 )) From ( select officer ,rank ,year ,month ,day , survey ,activity ,outcome ,mkt,non,totalmin,ICP ,date from [2014$] UNION ALL select officer ,rank ,year ,month ,day , survey ,activity ,outcome ,mkt,non,totalmin,ICP ,date from [2015$] UNION ALL select officer ,rank ,year ,month ,day , survey ,activity ,outcome ,mkt,non,totalmin,ICP ,date from [2016$] UNION ALL select officer ,rank ,year ,month ,day , survey ,activity ,outcome ,mkt,non,totalmin,ICP ,date from [2017$] )as table3 where officer is not null and officer <> '' and officer <> ' ' and date >= #" & fromDate & "# AND date<#" & toDate & "#" group by officer 

然后我发现SQL结果正在工作,但结果是不准确的。


后来,我尝试用硬代码进行testing。 例如,我尝试在2016年3月selectlogging。使用此where子句:

 where month=3 and year=2016 

没关系。

然后,我试图用下面的语句。 他们不是好的。

 where date >= #2016-03-01# AND date<#2016-04-01# 

where date between #2016-03-01# and #2016-04-01#

然后,我相信它应该是Excel或VBA中的date数据types问题。

然后,我试图做下面的这些testing:

对于Excel,原始数据types是date。 我更改为string,数字,一般…不好

 Date sample: 6/1/2016 String sample: 42375 Numeric sample: 42375.00 

对于VBA,我试图交换月份和date。 – >#2016-01-03#还不行

更多的信息可以参考由于SQLdate比较设置错误,SQL结果不准确

两件事情要尝试

fromDate后缀的时间点为零, toDate后缀的时间点为date的结尾。

 fromDate = 01/01/2016 00:00:00 toDate = 02/02/2016 23:59:59 

此外,如果可能的话,给这个月份作为三个字母的选项,我现在在我所有的工作中使用这个数据,因为很多数据到美国,他们做的是MM / DD / YYYY而不是英国的使用DD / MM / YYYY。 通过输出01 / Jan / 2016,用户或系统对于月份字段没有混淆。