views:

29

answers:

2

In Ruby on Rails I'm doing something like:

Appointment.find( :first, :conditions => "staff_id = #{staff_id} AND datetimefield = #{datetime}")

... where datetimefield is of course, a datetime field. But, I only want rows where the date is equal to a given day, say 2/12/2011. I don't care about the time. What's an easy way to do this?

Thanks!

A: 

One possibility is to query for the value being between midnight and 23:59:59 on the target day, something like:

datetimefield = BETWEEN #{datetime.strftime('%Y-%m-%d 00:00:00')} AND #{datetime.strftime('%Y-%m-%d 23:59:59')}"

Another option (not recommended if your column is indexed, because you'll almost certainly invalidate the use of that index) could be something like this:

"strftime('%Y-%m-%d', datetimefield) = #{datetime.strftime('%Y-%m-%d')}"
Mike Woodhouse
Awesome, thanks! Just slight correction for anyone else that comes along. in the first option I had to remove the '=', and add quotes around the dates to compare. So, ... AND datetimefield BETWEEN \'#{datetime.strftime('%Y-%m-%d 00:00:00')}\' AND \'#{datetime.strftime('%Y-%m-%d 23:59:59')}\'
99miles
A: 
SELECT * FROM TheTable WHERE DATE(TheField) = '2010-02-12';
dan04