PDA

View Full Version : selecting all events from a db within the next 7 days


danielwriter
09-11-2008, 07:49 PM
I am trying to select all the events from a msSQL db within the next 7 days starting from today. The select statement I am using is this:

sql = "select * from tEvents WHERE eventDate BETWEEN DateAdd(d,7,GetDate()) AND GetDate() ORDER BY eventDate"
set rsThisWeek = conn.execute(sql)

it doesn't throw me an error, however it isn't finding anything in the db. I know that there event dates that meet that criteria in the db. Whats weird is that if I change the select statement to get the events from the 7 previous days it works correctly. For example:

sql = "select * from tEvents WHERE eventDate BETWEEN DateAdd(d,-7,GetDate()) AND GetDate() ORDER BY eventDate"
set rsThisWeek = conn.execute(sql)

Anybody know why one would work and the other one doesn't?

Thanks in advance!

Spudhead
09-12-2008, 03:15 PM
Umm.. I don't have a MSSQL database I can test this against, so here goes with a probably idiot suggestion, but... does BETWEEN mind if the bigger value is before the lesser one? ie: have you tried switching it round so it says:
BETWEEN GetDate() AND DateAdd(d,7,GetDate())
?

:o

hinch
09-13-2008, 01:49 AM
you could use (syntax incorrect cba looking it up)

where DATECOL > now() and DATECOL < now()+7

danielwriter
09-15-2008, 07:04 PM
Thanks for the help.

I just needed to switch some of the tags around.
The correct tag is this:

select * from tEvents WHERE eventDate BETWEEN GetDate() AND DateAdd(d,7,GetDate()) ORDER BY eventDate