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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

finding records between dates problem

Status
Not open for further replies.

bigjerms

Technical User
Sep 15, 2005
149
US
Here is my query.

SELECT * FROM callhistory
WHERE date_format(callhistory.TimeStamp, '%m/%d/%Y') between '09/15/2004' and '01/08/2006'

This returns nothing.

If I change it to

SELECT * FROM callhistory
WHERE date_format(callhistory.TimeStamp, '%m/%d/%Y') between '09/15/2004' and '12/08/2006'

It works fine.

What am I doing wrong?
 
what you're doing wrong is attempting to use varchar strings as dates

you don't have to format a date, mysql knows it's a date (assuming, of course, that you have actually declared it as a DATE datatype)

try this --

SELECT * FROM callhistory
WHERE TimeStamp between '2004-09-15' and '2006-12-08'


r937.com | rudy.ca
 
I figured it out. You are right, but I messed up originally thinking it was having problems with comparing dates that were not padded in the month and day to 2 characters.

I now realize that mysql expects the year first when comparing I was feeding it dates with the month first which would mess up everything. This works ok until you you have two different years.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top