Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Range Problem

Status
Not open for further replies.

baden

Programmer
Feb 6, 2002
125
US
[MySQL] Having unsuccessful attempts at returing records that fall within a given date range...
Given two fields of either DATE or DATETIME types: {startEvent, endEvent} = {2/7/2005, 2/7/2005}

SQL:
SELECT * FROM table WHERE startDate <= '2/7/2005' AND endDate >= '2/7/2005' ORDER BY eventID

This returns nothing. I can just do a startdate > '2/7/2005' and that returns the record - but obviously not what we're trying to do here.

This does work w/ MS Access though:
SELECT * FROM table WHERE startDate <= #2/7/2005# AND endDate >= #2/7/2005# ORDER BY eventID


Any help please?!
 
You might need to discard the time part of your search fields as follows:

Code:
SELECT * 
FROM table 
WHERE 
   DATE(startDate) <= '2/7/2005' And
   DATE(endDate) >= '2/7/2005' 
ORDER BY eventID
 
Problem? MySQL date format differs. I forgot about a function I wrote a while ago - format ASP date to MySQL date... thus:

Code:
Function FormatSQLDate(dateTimeVal)
   'format ASP date values to MySQL dates
   FormatSQLDate = Year(dateTimeVal) & "-" & Month(dateTimeVal) & "-" & Day(dateTimeVal) & " " & Hour(dateTimeVal) & ":" & Minute(dateTimeVal) & ":" & Second(dateTimeVal)
   'Response.write FormatSQLDate & "<=="
End Function

Works great!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top