views:

33

answers:

2

Hello,

This should be fairly simple, but I can't get my head around it.

I have an event in my database with a startDate and an endDate.

I need to display this event (based on the current date) on every day the event occurs.

So if the event starts on the 3rd of May and finishes on the 7th of May, the SQL query must find it on every single day.

How can I achieve this?

SELECT * FROM events WHERE startDate ???

Thanks,

Tim

+3  A: 

Assuming SQL Server:

SELECT * 
FROM events 
WHERE getdate() between startDate and endDate
RedFilter
Ah, you beat me to the punch! ;)
EdgarVerona
+2  A: 

Oh, try:

SELECT * FROM events WHERE GetDate() BETWEEN startDate AND endDate

Essentially, you're telling the database to look up the current date (with the GetDate() function) and see if it lies between the start and end date of the given events!

Just to elaborate a bit, one of the things I remember in school that got to me was the use of functions in queries. SQL has a lot of built in functions like GetDate() that you can use pretty much anywhere in your query: in the WHERE clause to compare against values in the given query, in the SELECT clause if you need to do something to data that you want to output, etc... don't be afraid to use 'em! =)

EdgarVerona
the only caveat with GetDate() is that you can't use it in user-defined functions. stored procs are fine, though.
oedo
Ah, a good point as well!
EdgarVerona
Is there a solution for MYSQL?
Tim
Here, check this out:http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.htmlThat reference document will get you to all the functions you might be interested in, but in particular the URL is pointing to the Date and Time functions... have at it! =)
EdgarVerona