简体   繁体   中英

ms access sql query criteria between dates fields

in access2016, Have a table 'tbl_employee' with fields. (employee_name, emp_no, date_entry, date_access_created, UserHandledBY) eg

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.

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

Try with 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. 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 ((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. 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.

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.

So if you had 7 days in between those two date fields say it was going through from Monday to sunday. 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.

Hopefully that makes sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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