Friday, March 9, 2012

Query DateTime DataType for Current or Future Events

I have a sql server express 2005 database with a table named Events with a column named Date (datetime datatype). I want a query that will display all rows that are either current or future but not past. I suspect there is a simple way of doing this. As a Newbie searching archived threads this is what I have come up with so far. I determine the number of days from present:

SELECTDATEDIFF(day, Date,GETDATE())AS NumberOfDays

FROMEvents

This yields number of days from present with positive numbers in the past and negative numbers in the future. Thus setting a WHERE clause to <= 0 would limit my results to present or future events. Something like this:

SELECT*

FROM Events

WhereDATEDIFF(day, Date,GETDATE())AS NumberOfDays<= 0

The error message states: "Incorrect syntax near the keyword 'AS'"

This feels like a clumsy way to approach this problem, but I have to start where I am.

Any suggestions on how to proceed will be greatly appreciated.

SELECT*FROM EventsWhereDATEDIFF(day, Date,GETDATE())<= 0

or

select*FROM EventsWHERE Date>=GETDATE()

|||Thanks limno. Exactly what I needed.

No comments:

Post a Comment