开发者

Matching day in datetime field from sqlite

开发者 https://www.devze.com 2022-12-23 22:12 出处:网络
In Ruby on Rails I\'m doing something like: Appointment.find( :first, :conditions => \"staff_id = #{staff_id} AND datetimefield = #{datetime}\")

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 o开发者_开发百科nly 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!


SELECT * FROM TheTable WHERE DATE(TheField) = '2010-02-12';


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')}"
0

精彩评论

暂无评论...
验证码 换一张
取 消