Code:
...
WHERE
...
AND DateDiff(n, tblAssets.LastSeen, GetDate()) < 2*24*60
AND tblAssets.Uptime / 60 / 60 / 24 > 7
...
When dealing with time differences, I prefer to calculate to a finer resolution than what the difference is based on. If you don't care about that, you can calculate
DateDiff() in days.
When working with DateDiff(), you have to be careful of what you're asking for, what you're expecting returned, and what's actually returned.
Given:
- Last seen: 2021-02-17 12:01:03.977
- GetDate(): 2021-02-19 08:15:44.103
Then:
- DateDiff(n, LastSeen, GetDate()) / 60 / 24 = 1
- DateDiff(n, LastSeen, GetDate()) / 60.0 / 24 = 1.843055541
- DateDiff(d, LastSeen, GetDate())= 2
If you only care that it was last seen
at some point during the day two days ago, then calculating using days is sufficient. If you care that it was actually less than 48 hours ago, calculating the difference in hours or minutes is required.