SQL sql访问如何在日期之间返回

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3168529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 06:45:35  来源:igfitidea点击:

sql access how to return between dates

sqlms-accessvba

提问by l--''''''---------''''''''''''

How do I specify a date range in MS Access? Is the below query correct? Do I have to put "2/1/2010"in quotes? Or do I have to do something like date(2/1/2010)?

如何在 MS Access 中指定日期范围?以下查询是否正确?我必须加"2/1/2010"引号吗?还是我必须做类似的事情date(2/1/2010)

SELECT [Occurrence Number] as Fld
  FROM [Lab Occurrence Form]
 WHERE [Practice Code]="ACCIM"
   AND [1 0 Preanalytical (Before Testing)]="1.1 Specimen Mislabeled"
   AND ([Occurrence Date] Between 2/1/2010 and 2/28/2010);

the following gives me a type mismatch

以下给了我一个类型不匹配

SELECT [Occurrence Number] as Fld FROM [Lab Occurrence Form] WHERE [1 0 Preanalytical (Before Testing)]="1.1 Specimen Mislabeled" AND [Occurrence Date] between "1/1/2009" and "2/2/2010";

回答by Alex W

ms-access use the Jet engine which uses # for date literal:

ms-access 使用 Jet 引擎,它使用 # 作为日期文字:

SELECT Orders.*
  FROM Orders
 WHERE Orders.OrderDate Between #3/1/96# And #6/30/96#;

回答by shahkalpesh

AND ([Occurrence Date] Between #2/1/2010# and #2/28/2010#

This is how you tell Access, to interpret something as date time.

这就是您告诉 Access 将某些内容解释为日期时间的方式。

回答by hanima4

Ms access database uses the "#" for presenting dates. So if you want to write 13/12/2013 as ms access acceptable form then you have to write it as #13/12/2013#.

Ms access 数据库使用“#”表示日期。所以如果你想把 13/12/2013 写成 ms access 可接受的形式,那么你必须把它写成#13/12/2013#。

An example sql query for table called "test" with two fields id, and date.

具有两个字段 id 和 date 的名为“test”的表的示例 sql 查询。

select * from test where date=#13/12/2013#.

select * from test where date=#13/12/2013#。

An example of a sql query for vb.net 2008 to find the database records between two dates

vb.net 2008 查找两个日期之间的数据库记录的sql查询示例

"select * from info_session where i_date between # " & startingdate & " # and # " & enddate & " #"

"select * from info_session where i_date between # " & startingdate & " # and # " & enddate & " #"

回答by Vincenzo

The dates to use in select (in Microsoft) are defined as: "#"+month+"/"+day+"/" +year+"#"

在 select(在 Microsoft)中使用的日期定义为:“#”+month+“/”+day+”/”+year+“#”

the field day is a number 01,02---,31
the field month is a number 01,02 ...12
the field year is 2014, 2015 ...etc

字段日是数字 01,02---,31
字段月份是数字 01,02 ...12
字段年份是 2014, 2015 ...等

you can build the SQL field dinamically

您可以动态地构建 SQL 字段

es. in vbscript

es. 在脚本中

dt1="#"&month(date1)&"/"&day(Date1)&"/"&year(Date1)&"#"
dt2="#"&month(date2)&"/"&day(Date2)&"/"&year(Date2)&"#"

and then you can use the select in a field SQL

然后您可以在字段 SQL 中使用选择

where the table Orders has a field named OrderDate es.

其中表 Orders 有一个名为 OrderDate es 的字段。

SQL="select * from Orders where OrderDate Between " & dt1 & " and " dt2

now you can access the Database

现在您可以访问数据库