简体   繁体   English

ms访问日期字段之间的sql查询条件

[英]ms access sql query criteria between dates fields

in access2016, Have a table 'tbl_employee' with fields. 在access2016中,具有包含字段的表'tbl_employee'。 (employee_name, emp_no, date_entry, date_access_created, UserHandledBY) eg (employee_name,emp_no,date_entry,date_access_created,UserHandledBY),例如

JohnSmith, 1234, 7/19/2018, 7/24/2018, tim

Mickey, 321, 7/19/2018, 7/19/2018, kim

which where criteria to get list of all employee whose date_entry and date_access_created is >= 2 days. 获取date_entry和date_access_created为> = 2天的所有员工列表的条件。

select employee_name, emp_no, date_entry, date_access_created
from tbl_employee
where date_entry >= 2

Try with DateDiff : 尝试使用DateDiff

select 
    employee_name, emp_no, date_entry, date_access_created
from 
    tbl_employee
where 
    DateDiff("d", date_entry, date_access_created) >= 2

Net: 净:

I'm not sure exactly what you are asking, but if you are looking to have your query work where BOTH date_entry and date_access_created are equal to or larger than two, you can simply do that in your WHERE statement. 我不确定您要问的是什么,但是如果您希望在date_entry和date_access_created都等于或大于两个的情况下进行查询工作,则只需在WHERE语句中进行即可。 Look below: 往下看:

select employee_name, emp_no, date_entry, date_access_created
from tbl_employee
where ((date_entry >= 2) AND (date_access_created >= 2))

All I did was edit the WHERE line to look like this 我所做的就是编辑WHERE行,如下所示
where ((date_entry >= 2) AND (date_access_created >= 2))

Adding in the AND functionality, you can put in additional parameters on which to filter your results. 添加“与”功能后,您可以添加其他参数来过滤结果。

Please let me know if this achieved your expected results. 请让我知道这是否达到了您的预期结果。

@net As I mentioned in my earlier comment, it seems as though you mean to do a date diff. @net正如我在之前的评论中提到的那样,似乎您要进行日期比较。 If so then you could do 如果是这样,那么你可以做

WHERE DATEDIFF(DD, date_entry, date_access_created) >= 2

That will then say is that row greater than two days difference between the two dates specified. 这将表明该行在指定的两个日期之间相差两天以上。

The way it works is, it says how many days are there between those dates which in your example for the first row it would be 5 days difference. 它的工作方式是,说两个日期之间有多少天,在您的示例中,第一行的日期相差5天。

The full query would be: 完整的查询将是:

select employee_name, emp_no, date_entry, date_access_created
from tbl_employee
WHERE DATEDIFF(DD, date_entry, date_access_created) >= 2

Edit From Question In Comments: 从评论中的问题编辑:

Yes you can exclude weekends, actually I did this not long ago and used the most simple one I could find that worked. 是的,您可以排除周末,实际上我不久前就这样做了,并使用了我能找到的最简单的方法。

(DATEDIFF(dd, date_entry, date_access_created) + 1)
    -(DATEDIFF(wk, date_entry, date_access_created) * 2)
    -(CASE WHEN DATENAME(dw, date_entry) = 'Sunday' THEN 1 ELSE 0 END)
    -(CASE WHEN DATENAME(dw, date_access_created) = 'Saturday' THEN 1 ELSE 0 END)

I know it looks a lot different but, the way it works is the same as the first one I mentioned. 我知道它看起来有很大不同,但是它的工作方式与我提到的第一个相同。 It uses the same datediff parameters which are your start and end dates but the only bit extra it does is check along those days which days are sundays and which are Saturdays and if those days show up we wont to minus those from the overall count of days between the two dates. 它使用相同的datediff参数作为开始日期和结束日期,但它唯一要做的一点就是检查那些天是星期天和星期六,如果这些天出现了,我们就不会减去总天数在两个日期之间。

So if you had 7 days in between those two date fields say it was going through from Monday to sunday. 因此,如果您在这两个日期字段之间有7天的时间,请说它是从星期一到星期日。 It would say ok we have one Saturday there and one Sunday so that is 2 days we don't want in the overall count so it would do 7 - 2 = 5 days difference. 可以说,我们在那里有一个星期六,一个星期日,所以这是我们不希望有2天的总计数,所以它会相差7-2 = 5天。

Hopefully that makes sense. 希望这是有道理的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM