Query Expression

juneau730

Board Regular
Joined
Jun 7, 2018
Messages
111
Hello all,

In my data base, I have a query that I need to compare two dates, and then show only those that are older than 30 days. I currently have a Day Difference expression built, to find the integer between the dates and a greater than 30 criteria.

The dates would be a fixed date (Date of Scan) and a variable date (first_discovered)

Day Difference: Int([Date of Scan]-[first_discovered])

It appears to be working, but I wonder if there's a more effective method to do this same things?
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
If your values are just dates with no time values, then the INT function isn't needed at all (as that would just drop the decimal portion due to time).

And you don't actually need to store it in a calculation. You just just put it in criteria part of your query, i.e.
SQL:
WHERE [Date of Scan]-[first_discovered] > 30
 
Upvote 0
Not necessarily better, just more flexible: DateDiff("d",#01/01/2021#,#02/28/2021#)
With that you can specify days, weeks, months, years (there are about 10 options). A good function to know about even if you don't use it for this query. It can also return integers only when the returned value would otherwise be fractional. F'rinstance
datediff("d",#01/01/2021#,#03/28/2021#) will return 86 (days) but
datediff("m",#01/01/2021#,#03/28/2021#) will return 2 (months)
 
Upvote 0
If your values are just dates with no time values, then the INT function isn't needed at all (as that would just drop the decimal portion due to time).

And you don't actually need to store it in a calculation. You just just put it in criteria part of your query, i.e.
SQL:
WHERE [Date of Scan]-[first_discovered] > 30
Thank Joe4, unfortunately one of the dates does contain a time in it, but I am going to play around with your code and see if I can't make it work. I appreciate it
 
Upvote 0
Not necessarily better, just more flexible: DateDiff("d",#01/01/2021#,#02/28/2021#)
With that you can specify days, weeks, months, years (there are about 10 options). A good function to know about even if you don't use it for this query. It can also return integers only when the returned value would otherwise be fractional. F'rinstance
datediff("d",#01/01/2021#,#03/28/2021#) will return 86 (days) but
datediff("m",#01/01/2021#,#03/28/2021#) will return 2 (months)
Thanks Micron, I'll keep this in my list of great to know info.
 
Upvote 0
Thank Joe4, unfortunately one of the dates does contain a time in it, but I am going to play around with your code and see if I can't make it work. I appreciate it
Then just add the INT function, like you had originally, i.e.
SQL:
WHERE INT([Date of Scan]-[first_discovered]) > 30
or use the DATEDIFF solution that Micron suggested, with "d" to denote days.
 
Upvote 0
Solution
Then just add the INT function, like you had originally, i.e.
SQL:
WHERE INT([Date of Scan]-[first_discovered]) > 30
or use the DATEDIFF solution that Micron suggested, with "d" to denote days.
Thanks, you guys rock.
I am glad that I came across this forum, the folks here have been a huge help!
 
Upvote 0
You are welcome.
Glad we were able to help!
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,305
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top