The overall best query and where clause also depends on the type of stop.sched_arrive_early.
If that is a date, you also would cast DATEADD(day, 3, Getdate()) to a date or use CURDATE()+3, if your MSSQL Server version offers that.
sched_arrive_early is a datetime field, I tried using sched_arrive_early = CURDATE() + 3 and that didn't work, telling me 'CURDATE' is not a recognized built-in function name. [SQLSTATE: 42000 Error Code: 195]
Just do SELECT DATEADD(day, 3, Getdate()), you'll see that working.
The problem is not the calculation but the comparison. You typically need some BETWEEN comparison of a datetime being in a timespan.
An exception to that rule is, you always store midnight as time portion of the datetime, then it stands for a date rather than some point in time.
So next question: Is the stop.sched_arrive_early always set to a date at midnight? Or perhaps at 12pm?
If midnight is the case DATEADD(dd, DATEDIFF(dd, 0, getdate())+3, 0) will give you midnight of the date in 3 days, so what would then work is:
Code:
Where sched_arrive_early = DATEADD(dd, DATEDIFF(dd, 0, getdate())+3, 0)
That join can't come from that where condition. Most likely comes from another where clause or join condition ORed somehow with this. Or a union, subselect. Any complexity allowing data not fulfilling that condition but others.
You might post your whole query to get a clue. DATEADD(dd, DATEDIFF(dd, 0, getdate())+3, 0) will only result in current date + 3 days midnight and if data is not fulfilling that condition, it has to come in by fullfilling other conditions of your overall query.
I usually start with the hard-coded values:[tt]
Where stop.sched_arrive_early between '05/20/2016' and '05/24/2016'[/tt]
The dates are at midnight (time 00:00:00), so it will give you the whole day of 20th, 21st, 22nd, and 23rd until midnight.
Now you just need to replace the dates in quotes with the magic of
[tt]dateadd(dd, X, datediff(dd, 0, getdate())) [/tt]
Or you can try something like:
[tt]
Where stop.sched_arrive_early between CONVERT(date, GETDATE()) and CONVERT(date, GETDATE() + 4)[/tt]
Have fun.
---- Andy
A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.